I use MVVM pattern so my own control contains View and ViewModel.
ViewModel is connected with View by DataContext property. This makes problems with binding. Why?
Assume this situation:
I created new user control – for example – “SuperTextBox”. It have a property “SuperValue”.
And now I do something like that:
<Window>
<Window.DataContext>
<vm:WindowViewModel/>
</Window.DataContext>
<local:SuperTextBox SuperValue="{Binding Test}"/>
</Window>
I thought that “binding process” joins SuperTextBox.SuperValue with Window.DataContext.Test, but no, ‘binding process” joins SuperTextBox.SuperValue with SuperTextBox.DataContext.Test what is for me unnatural and misleading.
Other controls like “TextBox” I can use in above way because they do not have their DataContext.
How can I use MVVM pattern to creating UserControls keeping natural binding (to DataContext of parent control) ?
Edit:
I got many answers how binding to parent, but I know this earlier. The problem is – how can I create UserControl via MVVM patern (having ViewModel) and stay natural binding – default to parent DataContext.
I want to have ViewMoldel and still can binding like this:
<local:SuperTextBox SuperValue="{Binding Test}"/>
Is it possible?
I feel oddly answering on my question but…
In my own control i did something like that:
Now I can use “natural” binding outside of control and in control. 🙂