AncestorType in RelativeSource – Silverlight 5

By | April 19, 2011

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.

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

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.