Explain Ancestor relative source binding in WPF?

Answer

This relative binding helps to bind properties to the parent element properties. For example in the below XAML code we have a textbox which has two border’s as a parent. One border is having dark green and the other border is having dark red color as the border color.

The dark green color border is the parent element followed by dark red and the text box the child element at the end of the hierarchy.

<Border BorderBrush="DarkGreen">
<Border BorderBrush="DarkRed”>
<TextBox />
</Border>
</Border>

Below is how the WPF application looks like when it runs.

Now we want the background color of the text box to be binded to one of the parent border colors. To achieve the same we can use ancestor relative binding.

Below are some important properties in Ancestor type binding we need to know before we writing the binding code.

Property Description
AncestorType Which type of parent element is it?. Is it a border element , grid element etc.
AncestorLevel An element can have multiple parents. For example in the above XAML we have two parents one with red color and the other with green. So this property specifies which level of parent element we want to refer. Below is the figure which depicts the levels. The red color border is level 1 and the green color border is level 2.
So the nearest element becomes the first level and so on.
Binding This specifies which property we want to bind to. For the current example we want to bind to border’s brush color.

So the relative binding code with ancestor level 1 i.e. red looks as shown below. In case you are confused with any of the properties please refer to the previous table for more information.

<TextBox  Background="{Binding BorderBrush, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type Border}}}"/>

So now the complete XAML with parent border element looks as shown in the below code.

<Border BorderBrush="DarkGreen"><!-- Level 2 ->
<Border BorderBrush="DarkRed”><!-- Level 1 ->

<TextBox  removed="{Binding BorderBrush, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType=

{x:Type Border}}}"/>

</Border>
</Border>

Now if you run the above XAML code the textbox is binded with the back ground color of the first border. If you change the ancestor level to 2 textbox background color will change to green.

PreviousData

<ItemsControlItemsSource="{Binding}" Margin="10">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlockFontSize="14"FontWeight="bold"
 Margin="20"
 Text="{Binding Value}" removed="Aqua">
</TextBlock>
<TextBlockFontSize="14"FontWeight="bold"
 Margin="20"
 Text="{Binding
RelativeSource={RelativeSourcePreviousData},
 Path=Value}" removed="Blue">

</TextBlock>

</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>

publicclassItem : INotifyPropertyChanged
    {
privatedouble _value;

publicdouble Value
        {
get { return _value; }
set { _value = value; OnPropertyChanged("Value"); }
        }

        #regionINotifyPropertyChanged Members

publiceventPropertyChangedEventHandlerPropertyChanged;

        #endregion

protectedvoidOnPropertyChanged(stringPropertyName)
        {
if (null != PropertyChanged)
            {
PropertyChanged(this,
newPropertyChangedEventArgs(PropertyName));
            }
        }
    }
publicclassItems : ObservableCollection<Item>
    {
public Items()
        {
Add(newItem { Value = 80.23 });
Add(newItem { Value = 126.17 });
Add(newItem { Value = 130.21 });
Add(newItem { Value = 115.28 });
Add(newItem { Value = 131.21 });
Add(newItem { Value = 135.22 });
Add(newItem { Value = 120.27 });
Add(newItem { Value = 110.25 });
Add(newItem { Value = 90.20 });
        }
    }


All wpf Questions

Ask your interview questions on wpf

Write Your comment or Questions if you want the answers on wpf from wpf Experts
Name* :
Email Id* :
Mob no* :
Question
Or
Comment* :
 





Disclimer: PCDS.CO.IN not responsible for any content, information, data or any feature of website. If you are using this website then its your own responsibility to understand the content of the website

--------- Tutorials ---