0
votes

In my project, I have a Usercontrol which contains two child controls say a textbox and a button. A Common Style was written in App.xaml for Textbox. So this style will apply for that usercontrol's Textbox and in tat textbox when i tried to get the TemplatedParent it was null. How do i get the Usercontrol from the Textbox, so that the properties in the Usercontrol can be get in the textbox style.

1

1 Answers

0
votes

TemplatedParent can be used within ControlTemplate.

For Style you need to use RelativeSource with mode set to FindAncestor and AncestorType set to UserControl.

Suppose you want to set Text to Name of UserControl from TextBox Style, you can do this way:

<Style TargetType="TextBox">
   <Setter Property="Text"
           Value="{Binding Path=Name,
                           RelativeSource={RelativeSource Mode=FindAncestor, 
                                                   AncestorType=UserControl}}"/>
</Style>