WPF绑定数据源之RelativeSource
来源:互联网 发布:jre windows x64.exe 编辑:程序博客网 时间:2024/04/28 15:00
转自:
http://www.douban.com/note/217168378/
WPF绑定数据源之RelativeSource
一.FindAncestor
有时候我们不确定作为数据源的对象叫什么名字,但知道作为绑定源与UI布局有相对的关系,如下是一段XAML代码,说明多层布局控件中放置一个文本控件,来显示父级控件的名称。
1、XAML
Html代码:
<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="textBox1" FontSize="24" Margin="10"/>
</DockPanel>
</Grid>
</DockPanel>
</Grid>
2、后台代码
Csharp代码 :
RelativeSource rs = new RelativeSource(RelativeSourceMode.FindAncestor);
//设定为离自己控件最近的第二层父控件
rs.AncestorLevel = 2;
//设定父控件为Gird类型
rs.AncestorType = typeof(Grid);
//绑定源为Grid的名称
Binding binding = new Binding("Name") { RelativeSource=rs};
//将绑定的源放在文本显示内容中
this.textBox1.SetBinding(TextBox.TextProperty, binding);
3、以上后台代码等同于XAML中的
Html代码:
<TextBox x:Name="textBox1" FontSize="24" Margin="10" Text="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type Grid},AncestorLevel=2},Path=Name}"/>
二.TemplatedParent
TemplatedParent是RelativeSource的其中一种方式,使用该方式将使源元素成为模板目标类型—即TargetType;如果该绑定是在模板中,那么它的作为范围也只限于该模板.
例:
<Style TargetType="{x:Type local:TemplatedParent}">
<Setter Property="Background" Value="Green"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:TemplatedParent}">
<Grid>
<Ellipse>
<Ellipse.Fill>
<SolidColorBrush Color="{Binding Path=Background.Color,RelativeSource={RelativeSource TemplatedParent}}"/>
</Ellipse.Fill>
</Ellipse>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
这样绑定的源元素就指向local:TemplatedParent这个目标类型了,所以当你修改目标类型的背景颜色时,Ellipse也将跟随它变化。
三.Self
<Window x:Class="WpfApplication1.chap5_2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="chap5_2" Height="300" Width="300">
<Grid>
<Slider Name="slider"
Margin="4" Interval="1"
TickFrequency="1"
IsSnapToTickEnabled="True"
Minimum="0" Maximum="100"
ToolTip="{Binding RelativeSource ={ RelativeSource Self}, Path=Value}"/>
</Grid>
</Window>
其中Binding RelativeSource={RelativeSource Self}等价于Binding RelativeSource={x:Static RelativeSource.Self}
有时候我们不确定作为数据源的对象叫什么名字,但知道作为绑定源与UI布局有相对的关系,如下是一段XAML代码,说明多层布局控件中放置一个文本控件,来显示父级控件的名称。
1、XAML
Html代码:
<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="textBox1" FontSize="24" Margin="10"/>
</DockPanel>
</Grid>
</DockPanel>
</Grid>
2、后台代码
Csharp代码 :
RelativeSource rs = new RelativeSource(RelativeSourceMode.FindAncestor);
//设定为离自己控件最近的第二层父控件
rs.AncestorLevel = 2;
//设定父控件为Gird类型
rs.AncestorType = typeof(Grid);
//绑定源为Grid的名称
Binding binding = new Binding("Name") { RelativeSource=rs};
//将绑定的源放在文本显示内容中
this.textBox1.SetBinding(TextBox.TextProperty, binding);
3、以上后台代码等同于XAML中的
Html代码:
<TextBox x:Name="textBox1" FontSize="24" Margin="10" Text="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type Grid},AncestorLevel=2},Path=Name}"/>
二.TemplatedParent
TemplatedParent是RelativeSource的其中一种方式,使用该方式将使源元素成为模板目标类型—即TargetType;如果该绑定是在模板中,那么它的作为范围也只限于该模板.
例:
<Style TargetType="{x:Type local:TemplatedParent}">
<Setter Property="Background" Value="Green"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:TemplatedParent}">
<Grid>
<Ellipse>
<Ellipse.Fill>
<SolidColorBrush Color="{Binding Path=Background.Color,RelativeSource={RelativeSource TemplatedParent}}"/>
</Ellipse.Fill>
</Ellipse>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
这样绑定的源元素就指向local:TemplatedParent这个目标类型了,所以当你修改目标类型的背景颜色时,Ellipse也将跟随它变化。
三.Self
<Window x:Class="WpfApplication1.chap5_2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="chap5_2" Height="300" Width="300">
<Grid>
<Slider Name="slider"
Margin="4" Interval="1"
TickFrequency="1"
IsSnapToTickEnabled="True"
Minimum="0" Maximum="100"
ToolTip="{Binding RelativeSource ={ RelativeSource Self}, Path=Value}"/>
</Grid>
</Window>
其中Binding RelativeSource={RelativeSource Self}等价于Binding RelativeSource={x:Static RelativeSource.Self}
0 0
- WPF绑定数据源之RelativeSource
- WPF利用通过父控件属性来获得绑定数据源RelativeSource
- 在WPF中如何使用RelativeSource绑定
- WPF绑定各种数据源之object数据源
- WPF绑定各种数据源之xml数据源
- WPF 绑定各种数据源之Datatable
- WPF--MVVM之DataGrid数据源绑定
- WPF绑定XML数据源
- WPF数据源绑定方式
- WPF-DataGrid 绑定数据源
- WPF(Binding of RelativeSource)
- RelativeSource.TemplatedParent 属性wpf
- 数据绑定之 Source, RelativeSource和ElementName的不同
- DataGrid中的子控件Combox之数据源绑定(WPF)
- WPF绑定各种数据源之元素控件属性
- WPF--MVVM之DataGrid ComboBox数据源绑定枚举
- WPF\Silverlight 数据源绑定方式
- 相对数据源扩展(RelativeSource)
- 空杯心态,重新出发
- (stl)Valid Pattern Lock
- [Practical.Vim(2012.9)].Drew.Neil.Tip28 学习摘要
- Python学习之路18——控制语句,迭代器, 列表解析
- swift学习心得
- WPF绑定数据源之RelativeSource
- 《追风筝的人》
- 比CMD更强大的命令行WMIC
- 收集的一些网站
- Android速度仪表盘,速度评级小车动画(模仿电脑版360宽带测速器)
- 黑马程序员--IOS学习笔记总结 预处理指令(宏定义、条件编译、文件包含)
- 自考进行中—运筹学
- leetcode-46 Permutations
- iOS中变量命名引起的错误错误: [UILabel copyWithZone:]: unrecognized selector sent to instance