WPF中的binding(九)- 使用Binding的RelativeSource
2016-06-29 07:54
344 查看
我们进行Bingding时,如果明确知道数据源的Name,就能用Source或者ElementName进行绑定,但是有时候我们需要绑定的数据源可能没有明确的Name,此时我们就需要利用Bingding的RelativeSource进行绑定。
一、控件关联自身的属性
<Window x:Class="_6_27.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid x:Name="g1" Background="Red" Margin="10">
<DockPanel Name="d1" Background="Orange" Margin="10">
<Grid x:Name="g2" Background="Yellow" Margin="10">
<DockPanel Name="d2" Background="LawnGreen" Margin="10">
<TextBox Name="textbox" FontSize="24" Margin="10"
Text="{Binding RelativeSource={RelativeSource Mode=Self},Path=Name}"/>
</DockPanel>
</Grid>
</DockPanel>
</Grid>
</Window>
关联控件本身的属性时,只需要设置RelativeSource的Mode=Self即可。
其等效的cs代码:
RelativeSource rs = new RelativeSource();
rs.Mode = RelativeSourceMode.Self;
Binding binding = new Binding("Name") { RelativeSource = rs };
this.textbox.SetBinding(TextBox.TextProperty, binding);
运行效果如下:
二、控件关联其父级容器的属性
<Window x:Class="_6_27.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid x:Name="g1" Background="Red" Margin="10">
<DockPanel Name="d1" Background="Orange" Margin="10">
<Grid x:Name="g2" Background="Yellow" Margin="10">
<DockPanel Name="d2" Background="LawnGreen" Margin="10">
<TextBox Name="textbox" FontSize="24" Margin="10"
Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type Grid},AncestorLevel=1},Path=Name}"/>
</DockPanel>
</Grid>
</DockPanel>
</Grid>
</Window>
RelativeSource属性的数据类型为RelativeSource类,AncestorLevel指的是以Bingding目标控件为起点的层级偏移量,d2的偏移量是1,g2的偏移量是2,AncestorType指的是要找的目标对象的类型。值得注意的是AncestorLevel需要参考AncestorType使用,如上面设置了AncestorType={x:Type Grid},则Bingding在寻找时会忽略非Grid的控件,此时g2的偏移量是1,g1的偏移量是2,DockPanel被忽略。
一、控件关联自身的属性
<Window x:Class="_6_27.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid x:Name="g1" Background="Red" Margin="10">
<DockPanel Name="d1" Background="Orange" Margin="10">
<Grid x:Name="g2" Background="Yellow" Margin="10">
<DockPanel Name="d2" Background="LawnGreen" Margin="10">
<TextBox Name="textbox" FontSize="24" Margin="10"
Text="{Binding RelativeSource={RelativeSource Mode=Self},Path=Name}"/>
</DockPanel>
</Grid>
</DockPanel>
</Grid>
</Window>
关联控件本身的属性时,只需要设置RelativeSource的Mode=Self即可。
其等效的cs代码:
RelativeSource rs = new RelativeSource();
rs.Mode = RelativeSourceMode.Self;
Binding binding = new Binding("Name") { RelativeSource = rs };
this.textbox.SetBinding(TextBox.TextProperty, binding);
运行效果如下:
二、控件关联其父级容器的属性
<Window x:Class="_6_27.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid x:Name="g1" Background="Red" Margin="10">
<DockPanel Name="d1" Background="Orange" Margin="10">
<Grid x:Name="g2" Background="Yellow" Margin="10">
<DockPanel Name="d2" Background="LawnGreen" Margin="10">
<TextBox Name="textbox" FontSize="24" Margin="10"
Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type Grid},AncestorLevel=1},Path=Name}"/>
</DockPanel>
</Grid>
</DockPanel>
</Grid>
</Window>
RelativeSource属性的数据类型为RelativeSource类,AncestorLevel指的是以Bingding目标控件为起点的层级偏移量,d2的偏移量是1,g2的偏移量是2,AncestorType指的是要找的目标对象的类型。值得注意的是AncestorLevel需要参考AncestorType使用,如上面设置了AncestorType={x:Type Grid},则Bingding在寻找时会忽略非Grid的控件,此时g2的偏移量是1,g1的偏移量是2,DockPanel被忽略。
相关文章推荐
- osgi + felix example2b编写
- profile
- 【直视骄阳】遗愿清单
- Java面向对象概念
- 关于promise
- C#中一些常用的加密和哈希处理
- Nodejs监控文件内容变化并获取最新添加的内容
- Xcode各版本官方下载及百度云盘下载, Mac和IOS及Xcode版本历史.
- linux虚拟机网络不通,重启后还是不行
- 【Maven】快速走进Maven的世界
- c++运算符的重载
- Codeforces 163A Substring and Subsequence
- android 加载网络数据(1)
- 事务的简单应用
- qemu 跑内核和文件系统
- C++中32位单精度浮点数有效数字是多少位?
- 第三方支付接口对接基本流程
- Unity3d程序必备设计模式六大原则
- Memcache及telnent命令详解
- 灵活且强大的RecyclerViewAdapter源码分析