RelativeSource
当一个Binding有明确的数据来源时可以通过为Source或ElementName赋值的办法让Binding与之关联,有的时候由于不能确定Source的对象叫什么名字,但知道它与作为Binding目标的对象在UI布局上有相对关系,比如控件自己关联自己的某个数据、关联自己某级容器的数据,就要使用Binding的RelativeSource属性。RelativeSource属性的数据类型为RelativeSource类,通过这个类的几个静态或非静态属性可以控制它搜索相对数据源的方式。
下面这个界面是在多层布局控件中放置着一个TextBox
<Grid x:Name="g1" Background="Red" Margin="">
<DockPanel x:Name="d1" Background="Orange" Margin="">
<Grid x:Name="g2" Background="Yellow" Margin="">
<DockPanel x:Name="d2" Background="LawnGreen" Margin="">
<TextBox x:Name="textBox1" FontSize="" Margin=""></TextBox>
</DockPanel>
</Grid>
</DockPanel>
</Grid>
使用如下代码,将TextBox控件向外,从第一层开始寻找,找到的第一个Grid对象的Name与TextBox控件绑定
RelativeSource rs = new RelativeSource();
rs.Mode = RelativeSourceMode.FindAncestor;
rs.AncestorLevel = ;
rs.AncestorType = typeof(Grid); Binding binding = new Binding();
binding.RelativeSource = rs;
binding.Path = new PropertyPath("Name");
textBox1.SetBinding(TextBox.TextProperty, binding);
AncestorLevel属性指的是以Binding目标控件为起点的层级偏移量,d2的偏移量是1,g2的偏移量是2,以此类推,AncestorType属性告诉Binding寻找哪个类型的对象作为自己的源,不是这个类型的对象会被跳过
与之等价的XAML代码是
Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorLevel=1, AncestorType={x:Type Grid}}, Path=Name}"
RelativeSource的更多相关文章
- 背水一战 Windows 10 (19) - 绑定: TemplateBinding 绑定, 与 RelativeSource 绑定, 与 StaticResource 绑定
[源码下载] 背水一战 Windows 10 (19) - 绑定: TemplateBinding 绑定, 与 RelativeSource 绑定, 与 StaticResource 绑定 作者:we ...
- [WPF系列-高级TemplateBinding vs RelativeSource TemplatedParent]
What is the difference between these 2 bindings: <ControlTemplate TargetType="{x:Type Button ...
- 绑定: TemplateBinding 绑定, 与 RelativeSource 绑定, 与 StaticResource 绑定
介绍背水一战 Windows 10 之 绑定 TemplateBinding 绑定 与 RelativeSource 绑定 与 StaticResource 绑定 示例1.演示 TemplateBin ...
- 重新想象 Windows 8 Store Apps (52) - 绑定: 与 Element Model Indexer Style RelativeSource 绑定, 以及绑定中的数据转换
[源码下载] 重新想象 Windows 8 Store Apps (52) - 绑定: 与 Element Model Indexer Style RelativeSource 绑定, 以及绑定中的数 ...
- 【WPF】WPF通过RelativeSource绑定父控件的属性
1.后台代码实现绑定父控件的属性 RelativeSource rs = new RelativeSource(RelativeSourceMode.FindAncestor); //设定为离自己控件 ...
- WPF的控件Binding的ElementName/RelativeSource具体用法
<TextBlock Name="_txtSickBedNo" FontStyle="Normal" Foreground="Black&quo ...
- 数据绑定(八)使用Binding的RelativeSource
当一个Binding有明白的数据来源时能够通过为Source或ElementName赋值的办法让Binding与之关联,有的时候因为不能确定Source的对象叫什么名字,但知道它与作为Binding目 ...
- Binding 中 Elementname,Source,RelativeSource 三种绑定的方式
在WPF应用的开发过程中Binding是一个非常重要的部分. 在实际开发过程中Binding的不同种写法达到的效果相同但事实是存在很大区别的. 这里将实际中碰到过的问题做下汇总记录和理解. 1. so ...
- RelativeSource的用法
绑定自身的数据 <Style TargetType="Button"> <Setter Property="Width" Value=&quo ...
- Binding.RelativeSource 属性
Binding.RelativeSource 属性说明: 通过指定绑定源相对于绑定目标的位置,获取或设置绑定源. 此属性通常用于将对象的某个属性绑定到该对象的另一个属性,或用于在样式或模板中定义绑定. ...
随机推荐
- 理解JAVA与C的运行机制
1.java的运行机制 java的编译过程,将java的源程序(扩展名为.java的文件),由java编译程序将java的字节码文件(.class文件)在jvm上运行,机器码有cpu运行, jvm编译 ...
- J.U.C 系列 Tools之Executors
上个章节说了Tools中的其他四个工具类,本节我们来看一看工具类中的老大Executors,为什么说它是老大,肯定是因为他的功能最多最强大. 一 Executors是什么 Executors 是一个线 ...
- 数据库路由中间件MyCat - 源代码篇(2)
此文已由作者张镐薪授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 2. 前端连接建立与认证 Title:MySql连接建立以及认证过程client->MySql:1.T ...
- USACO Section2.2 Subset Sums 解题报告 【icedream61】
subset解题报告------------------------------------------------------------------------------------------ ...
- leetcode 【 Linked List Cycle 】 python 实现
题目: Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using ...
- Windows Server 2008 R2 集群(OpenService “RemoteRegistry” 失败)笔记
OpenService “RemoteRegistry” 失败. 我在创建验证域控服务器[系统]类别中 看到错误日志 我在域控服务器去看,在 computers 里面 是有这台 计算机,但是为什么不行 ...
- SQL + Python 面试题:之二(难度:中等)
SQL + Python 面试题:之二(难度:中等)
- tomcat运行solr
https://blog.csdn.net/u010346953/article/details/67640036
- ansible自动安装jdk
脚本功能:安装jdk 测试环境:CentOS6.7 说明: 1.卸载系统自带的openjdk,重新安装Oracle jdk,支持Hotspot,性能更好,更稳定. 2.jdk软件包按文档说明进行定制( ...
- Vue_初识
前端三大框架: vue:开发效率相当高了. angalar:适合做后台管理系统,入手容易,但是越往后会越难受. react:虚拟dom(渲染内存中存储的dom,经过操作后,才会去渲染浏览器的真实dom ...