其他分享
首页 > 其他分享> > 使用Binding的RelativeSource

使用Binding的RelativeSource

作者:互联网

  当一个Binding有明确的数据来源时,我们可以通过为Source或ElementName赋值的办法让Binding与之关联。有些时候我们不能确定作为Source的对象叫什么名字,但知道它与作为Binding目标的对象在UI布局上的相对关系,比如控件自己关联自己的某个数据、关联自己某级容器的数据。这时候我们就要使用Binding的RelativeSource属性。

  RelativeSource属性的数据类型为RelativeSource类,通过这个类的几个静态或非静态属性,我们可以控制它搜索相对数据源的方式。其中:

       • RelativeSource类的Mode属性的类型是RelativeSourceMode枚举,它的取值是:PreviousData、TemplateParent、Self和FindAncestor。RelativeSource类还有三个静态属性:PreviousData、Self和TemplateParent,它们的类型是RelativeSource类。实际上着三个静态属性就是创建一个RelativeSource实例、把实例的Mode属性设置为相应的值,然后返回这个实例。之所以准备着3个静态属性是为了在XAML代码里直接获取RelativeSource实例。

  • AncestorLevel属性指的是以Binding目标控件为起点的层级偏移量

  • AncestorType属性告诉Binding寻找哪个类型的对象作为自己的源,不是这个类型的对象就会被跳过。

<!--多层布局控件内放置一个TextBox-->
<Window x:Class="WPFDemo.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="RelativeSource" Height="210" Width="210">
    <Grid x:Name="g1" Background="Red" Margin="10">
        <DockPanel x:Name="d1" Background="Orange" Margin="10">
            <Grid x:Name="g2" Background="Yellow" Margin="10">
                <DockPanel x:Name="d2" Background="LawnGreen" Margin="10">
                    <TextBox x:Name="txt" FontSize="24" Margin="10"/>
                </DockPanel>
            </Grid>
        </DockPanel>
    </Grid>
</Window>
public MainWindow()
{
    InitializeComponent();
//把TextBox的Text属性关联到外层容器的Name属性上
RelativeSource rs = new RelativeSource(RelativeSourceMode.FindAncestor);
//AncestorLevel属性指的是以Binding目标控件为起点的层级偏移量
rs.AncestorLevel = 1;
//AncestorType属性告诉Binding寻找哪个类型的对象作为自己的源,不是这个类型的对象就会被跳过。
    rs.AncestorType = typeof(Grid);
    Binding binding = new Binding("Name") { RelativeSource =rs};
    this.txt.SetBinding(TextBox .TextProperty ,binding);
}
<!--把TextBox的Text属性关联到外层容器的Name属性上-->
<TextBox x:Name="txt" FontSize="24" Margin="10" Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type Grid},AncestorLevel=1},Path=Name}"/>

  Binding从自己的第一层依次向外找,找到第一个Grid类型对象后把它当作自己的源

public MainWindow()
{
    InitializeComponent();
//如果TextBox需要关联自身的Name属性
    RelativeSource rs = new RelativeSource();
    rs.Mode = RelativeSourceMode.Self;
    //rs.AncestorLevel = 1;
    //rs.AncestorType = typeof(Grid);
    Binding binding = new Binding("Name") { RelativeSource = rs };
    this.txt.SetBinding(TextBox.TextProperty, binding);
}
<TextBox x:Name="txt" FontSize="24" Margin="10" Text="{Binding RelativeSource={RelativeSource Mode=Self}},Path=Name}"/>

  

标签:rs,使用,Binding,RelativeSource,AncestorType,new,属性
来源: https://www.cnblogs.com/BigLinger/p/12540143.html