Set parent control's property with change in child control's property value in XAML (Using AncestorType binding)

In XAML, one can set parent's property from the change in a property of child control. To do so we can use AncestorType binding for the child control.

Consider an example we have a StackPanel having a TextBox. Which ever color user enters in the textbox must be reflected in the StackPanel's Background.

XAML

<StackPanel Orientation="Vertical" x:Name="stk">             <StackPanel Orientation="Horizontal">                 <TextBlock Text="Enter Color:"></TextBlock>   <TextBox x:Name="colorName" Width="150" BorderBrush="Brown"      Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor,         AncestorType={x:Type StackPanel}}, Path=Background,Mode=OneWayToSource}">   </TextBox><TextBox Text="Color Format = ARGB"></TextBox>             </StackPanel> </StackPanel>

Output




as we are seeing in the output whichever color we are inserting in the text box is being set as background color of the stackpanel.