Synchronizing control properties with the parent control properties in WPF

Sometimes you need synchronize the properties of a control with the properties of the parent control, in order to accomplish this you can use a binding by element name. In the example below you can see how to make the size of a control equal to its parent control by using binding:




      Width="{Binding RelativeSource=

                {RelativeSource FindAncestor,

                AncestorType={x:Type Canvas}},


      Height="{Binding RelativeSource=

                {RelativeSource FindAncestor,

                AncestorType={x:Type Canvas}},





  • Thanks. It helped me set the data context of a button column of the DataGrid.

  • This works if you know what type the parent is. What if the child is not aware of that information, but does know that the parent has Height and Width properties?

  • super ! , this helped me a lot while enlarging highlited drop down item content :
    &nbsp; &nbsp;&lt;Style TargetType="{x:Type ComboBoxItem}"&gt;
    &nbsp; &nbsp; &nbsp; &nbsp;&lt;Style.Resources&gt;
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Gray" /&gt;
    &nbsp; &nbsp; &nbsp; &nbsp;&lt;/Style.Resources&gt;
    &nbsp; &nbsp; &nbsp; &nbsp;&lt;Setter Property="MinWidth" Value="{Binding RelativeSource= {RelativeSource FindAncestor, AncestorType={x:Type Grid}},Path=ActualWidth}" /&gt;
    &nbsp; &nbsp;&lt;/Style&gt;

  • &lt;RadioButton Canvas.Left="25" Canvas.Top="19" Height="16" Name="radioButton3" Width="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type Canvas}},Path=ActualWidth}"&gt;RadioButton&lt;/RadioButton&gt;
    it is giving me an error that " Ancestor type must be specified for RElative Source in Find Ancestor mode"

  • Thanks, short and to the point, I finally understand.

  • Thanks, I was really trying to solve this problem, It looks simple and as per my requirement......

Comments have been disabled for this content.