Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass through wpf dependency property of usercontrol to child control

I have a custom MyControl : UserControl with dependency property

string Text

Inside the MyControl in XAML I have a TextBox.

I wish to bind the Text dependency property of MyControl to the Text dependency property of the TextBox.

What is the best way to do this? Can I declare the dependency property of MyControl to pass through to the child depenendency property?

like image 571
bradgonesurfing Avatar asked Sep 19 '25 22:09

bradgonesurfing


1 Answers

The easiest way is to assign a x:Name="root" attribute to the root of your MyControl.xaml file, and then use a binding like this for your TextBox:

<TextBox Text="{Binding Text, ElementName=root}" />

(You can specify your own name for root.)

like image 185
Dan Puzey Avatar answered Sep 22 '25 19:09

Dan Puzey