AncestorType in RelativeSource – Silverlight 5

WPF has a facility to search the AncestorType for a control from a child control such that it will find the next parent control that matches the Ancestor Type defined. Silverlight implements this feature to enable you to search your ancestor from child control from your visual tree and bind the child property with it. Lets take a look into it.

<DataTemplate>
<TextBox Width=&quot;{Binding Width RelativeSource={RelativeSource AncestorType=Border, AncestorLevel=5}} />
<DataTemplate>


Say you have a TextBox defined within the DataTemplate and you want to bind the Width of the TextBox to the width of the control somewhere outside it, you can use RelativeSource Markup extension to find it from the Visual tree.

Abhishek Sur

Abhishek Sur is a Microsoft MVP since year 2011. He is an architect in the .NET platform. He has profound theoretical insight and years of hands on experience in different .NET products and languages. He leads the Microsoft User Group in Kolkata named KolkataGeeks, and regularly organizes events and seminars in various places for spreading .NET awareness. He is associated with the Microsoft Insider list on WPF and C#, and is in constant touch with product group teams. He blogs at http://www.abhisheksur.com His Book : Visual Studio 2012 and .NET 4.5 Expert Development Cookbook. Follow Abhishek at Twitter : @abhi2434