Binding the RelativeSource
You can bind RelativeSouce of four modes, which are Self, TemplatedParent, FindAncestor and PreviousData. Here I will demonstrate the bindings of the first three modes. (PreviousData is excluded.)
Self refers to the target element.
TemplatedParent refers to the target element’s Parent which consumes the template.
FindAncestor refers to the target element’s Ancestor of a given type.
========================== XAML ============================
<Window … Title=”Window1″ …>
<StackPanel Name=”TheOuterStackPanel”>
<Button Content=”MySampleButton” ToolTip=”Still the SampleButton” Name=”SampleButton”>
<Button.Template>
<ControlTemplate x:Name=”theControlTemplate”>
<StackPanel Name=”theInnerStackPanel”>
<TextBlock Background=”Red” Text=”{Binding RelativeSource={RelativeSource Self}, Path=Background}”/> // the hex value of red
<TextBlock Background=”Aquamarine” Text=”{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Name}”/> // SampleButton
<Grid>
<TextBlock Background=”Khaki” Text=”{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Content}”/> // MySampleButton
</Grid>
<TextBlock Background=”ForestGreen” Text=”{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Button}}, Path=ToolTip}”/> // Still the SampleButton
<TextBlock Background=”Cornsilk” Text=”{Binding RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type StackPanel}}, Path=Name}”/> // TheInnerStackPanel
<TextBlock Background=”Chartreuse” Text=”{Binding RelativeSource={RelativeSource FindAncestor, AncestorLevel=2, AncestorType={x:Type StackPanel}}, Path=Name}”/> // TheOuterStackPanel
<TextBlock Background=”Cyan” Text=”{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=Title}”/> // Window1
</StackPanel>
</ControlTemplate>
</Button.Template>
</Button>
</StackPanel>
</Window>
Note: If binding the relative source happens in a data template, it seems only parent’s or ancestor’s content can be bound. In a control template like the sample above, such limit does not exist.
Note1: Such relative binding can be realized by pointing to the source element by name. However, if binding is defined in a template, which will probably be saved in a separate ResourceDictionary, Binding the RelativeSource seems the best option
Great post martin, thanks for putting this up. Just discovered your blog by way of google and I’m pleasantly surprised by your tone of voice and mix of subjects. Well, keep it up I guess 🙂
Adriaan said this on September 25, 2007 at 5:25 am |
Great article, just what i needed, keep up the good work!:)
antonio said this on September 1, 2008 at 6:21 am |
Man I wish we had this in Silverlight too.
Morten said this on November 25, 2008 at 10:40 pm |
Super awesome post dude.
omprakash said this on June 18, 2009 at 9:59 am |
This is awesome.
Suresh said this on August 31, 2009 at 9:15 pm |
[…] Binding the RelativeSource […]
Markus Tamm » Blog Archive » Links 24.02.2010 said this on February 24, 2010 at 4:47 am |
It was very much of useful to me
David said this on April 8, 2010 at 7:18 am |
Wow, this brief sample taught me more about RelativeBinding than hours of sifting through MSDN documentation. Great job Martin!
Carlos said this on May 21, 2010 at 8:33 am |
simply awesome example for showing up relative source fundamentals
anurag said this on July 27, 2011 at 2:35 pm |
very good explanation. Well done. Thanks!
E.T. said this on April 13, 2012 at 11:17 am |
Nice Post. Very Good Explanation.
Thanks for this
project file said this on April 25, 2013 at 5:20 am |