ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

使用Binding的RelativeSource

2020-03-21 17:02:07  阅读:262  来源: 互联网

标签:rs 使用 Binding RelativeSource AncestorType new 属性


  当一个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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有