RelativeSource Demo
April 10, 2006
New York City
I couldn't find any good examples of using RelativeSource in a Binding, so here's a RelativeSourceDemo.xaml file that has three of them. The file is also listed here:
-
<!-- =====================================================
RelativeSourceDemo.xaml (c) 2006 by Charles Petzold
===================================================== -->
<StackPanel xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
TextBlock.FontSize="12" >
<StackPanel Orientation="Horizontal"
HorizontalAlignment="Center">
<TextBlock Text="This TextBlock has a FontFamily of " />
<TextBlock Text="{Binding RelativeSource={RelativeSource self},
Path=FontFamily}" />
<TextBlock Text=" and a FontSize of " />
<TextBlock Text="{Binding RelativeSource={RelativeSource self},
Path=FontSize}" />
</StackPanel>
<StackPanel Orientation="Horizontal"
HorizontalAlignment="Center">
<TextBlock Text="This TextBlock is inside a StackPanel with " />
<TextBlock Text=
"{Binding RelativeSource={RelativeSource
AncestorType={x:Type StackPanel}},
Path=Orientation}" />
<TextBlock Text=" orientation" />
</StackPanel>
<StackPanel Orientation="Horizontal"
HorizontalAlignment="Center">
<TextBlock Text="The parent StackPanel has " />
<TextBlock Text=
"{Binding RelativeSource={RelativeSource
AncestorType={x:Type StackPanel}, AncestorLevel=2},
Path=Orientation}" />
<TextBlock Text=" orientation" />
</StackPanel>
</StackPanel>
It's not very exciting, but at least it works with the February CTP. Notice the comma in front of AncestorLevel, which doesn't appear in the documentation.