Explain Ancestor relative source binding in WPF ? ( WPF Interview questions with answers)

Before this WPF interview question it’s very much possible the interviewer can ask you to explain the basics of relative source binding. You can read the basics of WPF relative binding from http://www.dotnetinterviewquestions.in/article_explain-wpf-relative-binding-relative-resource-net-wpf-interview-questions-with-answers_201.html

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.

image 1

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.

image 2

image 3

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 Background=”{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.

image 4

We would advise to see WPF interview question videos published by www.questpond.com which covers all WPF chapters in detail in a quick time so that you are ready for WPF interviews.

Below video explain what is WPF and how they differ from Windows form

Advertisements

About c# and .NET Interview questions

This blog is for developers who want to crack .NET and C# interviews. It has all tips and tricks needed to crack .NET interviews , C# interview , SQL Server interview , Java interview , WCF Interview , Silverlight interview , WPF interview , LINQ interview , Entity framework Interview. Do not forget to watch our Learn step by step video series. Learn MVC in 16 hours:- https://www.youtube.com/watch?v=Lp7nSImO5vk Learn AngularJS Step by Step:- https://www.youtube.com/watch?v=0kmdjqgO9IY Learn Design Pattern in 8 hours:- https://www.youtube.com/watch?v=YDobmucohqk Learn C# and .NET in 60 days:- https://www.youtube.com/watch?v=yh2SrzCkNQA Learn MSBI in 32 hours:- https://www.youtube.com/watch?v=mGPJx3ocFgg Learn SharePoint Step by Step in 8 hours:- https://youtu.be/C2fW76SwJNU
This entry was posted in Uncategorized and tagged , , , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s