今天看了一些wpf 关于binding的知识,其中一个是element path形式,这种形式相对简单,而source path 则稍微复杂,刚开始看的时候,觉得RelativeSource path是看不懂,后来看了一些例子才知道它的作用,是banding自身或与自身相关的数据。
binding的东西有Self FindAncestor PreviousData TemplatedParent 根据意思就知道其绑定的模式。
看例子
相对数据源扩展(RelativeSource)
在数据绑定时,有时候需要把目标对象绑定到和目标对象自身有相关关系的对象上,这时就需要用到相对数据源扩展。例如:
- <Window x:Class="Yingbao.Chapter2.RelativeEx.AppWin"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- Title="相对绑定" Height="100" Width="300">
- <StackPanel Orientation="Horizontal"
- HorizontalAlignment="Center">
- <TextBlock FontSize="20" Text="{Binding
- RelativeSource={RelativeSource self}, Path=FontSize}"/>
- <TextBlock Margin="10,1,1,5" FontSize="20" Text="{Binding
- RelativeSource={RelativeSource AncestorType={x:Type
- StackPanel}}, Path=Orientation}"/>
- </StackPanel>
- </Window>
在这个例子中,笔者使用了两个相对数据源扩展,第一个TextBlock的Text绑定到自身的字体大小上;第二个TextBlock的Text则绑定到其父节点StackPanel的Orientation属性上。这段XAML的运行结果如图2-5所示。
![]() |
转载于:
http://book.51cto.com/art/201001/179748.htm