RelativeSource设定绑定方向
1 <Window x:Class="Yingbao.Chapter2.RelativeEx.AppWin" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 Title="相对绑定" Height="100" Width="300"> 5 <StackPanel Orientation="Horizontal" 6 HorizontalAlignment="Center"> 7 <TextBlock FontSize="20" Text="{Binding 8 RelativeSource={RelativeSource self}, Path=FontSize}"/> 9 <TextBlock Margin="10,1,1,5" FontSize="20" Text="{Binding 10 RelativeSource={RelativeSource AncestorType={x:Type 11 StackPanel}}, Path=Orientation}"/> 12 </StackPanel> 13 </Window>
在这个例子中,笔者使用了两个相对数据源扩展,第一个TextBlock的Text绑定到自身的字体大小上;第二个TextBlock的Text则绑定到其父节点StackPanel的Orientation属性上。