Pass wpf dependency property to usercontrol for child control
I have a custom MyControl: UserControl with a dependency property
string Text
Inside MyControl in XAML I have a TextBox.
I want to bind the Text dependency property of MyControl to the text dependency property of the TextBox .
What's the best way to do this? Can I declare the MyControl dependency property to go to the child dependency property?
The easiest way is to assign an attribute x:Name="root"
to the root of your file MyControl.xaml
, then use an anchor like this for yours TextBox
:
<TextBox Text="{Binding Text, ElementName=root}" />
(You can provide your name for root
.)
My answer here details a good example of how you can do this. Essentially, your controls are bound to properties in viewmodels, and the child view model has a dependency property that allows you to bind the child control and can output a value to the child view model. The example is in Silverlight, but the implementation is the same for WPF.