我如何使用WPF绑定的RelativeSource,有哪些不同的用例?
当前回答
在WPF中,RelativeSource绑定公开了三个属性来设置:
1. Mode:这是一个枚举,可以有四个值:
a. PreviousData(value=0):它将属性的上一个值赋给 约束函数 b. TemplatedParent(value=1):在定义的模板时使用 任何控件,并希望绑定到控件的值/属性。 例如,定义ControlTemplate:
<ControlTemplate>
<CheckBox IsChecked="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Value, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
</ControlTemplate>
c. Self(value=2):当我们想从一个Self或Self的属性绑定时。 例如:在设置复选框上的命令时,将复选框的选中状态发送为CommandParameter
<CheckBox ...... CommandParameter="{Binding RelativeSource={RelativeSource Self},Path=IsChecked}" />
d. FindAncestor(value=3):当想从父控件绑定时 在可视化树中。 例如:在记录中绑定一个复选框,如果选中了网格,如果选中了标题复选框
<CheckBox IsChecked="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type iDP:XamDataGrid}}, Path=DataContext.IsHeaderChecked, Mode=TwoWay}" />
2. 祖宗类型:当模式是find祖宗,然后定义什么类型的祖先
RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type iDP:XamDataGrid}}
3.祖宗级别:当模式是FindAncestor时,那么祖先的级别是什么(如果在可视树中有两个相同类型的父)
RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type iDP:XamDataGrid, AncestorLevel=1}}
以上是RelativeSource绑定的所有用例。
这里有一个参考链接。
其他回答
不要忘记TemplatedParent:
<Binding RelativeSource="{RelativeSource TemplatedParent}"/>
or
{Binding RelativeSource={RelativeSource TemplatedParent}}
如果你想绑定到对象上的另一个属性:
{Binding Path=PathToProperty, RelativeSource={RelativeSource Self}}
如果你想获取一个祖先的属性:
{Binding Path=PathToProperty,
RelativeSource={RelativeSource AncestorType={x:Type typeOfAncestor}}}
如果你想在父模板上获得一个属性(所以你可以在一个ControlTemplate中做2种方式绑定)
{Binding Path=PathToProperty, RelativeSource={RelativeSource TemplatedParent}}
或者更短(这只适用于OneWay绑定):
{TemplateBinding Path=PathToProperty}
Binding RelativeSource={
RelativeSource Mode=FindAncestor, AncestorType={x:Type ItemType}
}
...
RelativeSource的默认属性是Mode属性。这里给出了一组完整的有效值(来自MSDN):
PreviousData Allows you to bind the previous data item (not that control that contains the data item) in the list of data items being displayed. TemplatedParent Refers to the element to which the template (in which the data-bound element exists) is applied. This is similar to setting a TemplateBindingExtension and is only applicable if the Binding is within a template. Self Refers to the element on which you are setting the binding and allows you to bind one property of that element to another property on the same element. FindAncestor Refers to the ancestor in the parent chain of the data-bound element. You can use this to bind to an ancestor of a specific type or its subclasses. This is the mode you use if you want to specify AncestorType and/or AncestorLevel.
值得注意的是,对于那些无意中发现Silverlight的想法的人:
Silverlight只提供了这些命令的一个简化子集
我刚刚发布了另一个在Silverlight中访问父元素的DataContext的解决方案。它使用Binding ElementName。
推荐文章
- 如何从字符串中删除新的行字符?
- 是否已经添加了事件处理程序?
- Lookup()和Dictionary(Of list()的区别
- 如何从我的c#代码运行EXE文件?
- Node.js vs .Net性能
- .NET控制台应用程序中的全局异常处理程序
- 枚举上最常见的c#位操作
- PowerShell脚本在机器上返回。net框架的版本?
- WPF数据网格底部空行
- 用c#创建一个空文件
- 如何改变列表<T>数据IQueryable<T>数据
- 您可以使用反射来查找当前正在执行的方法的名称吗?
- 流。Seek(0, SeekOrigin.Begin)或Position = 0
- 如果查询为空,则最大返回值
- 传递一个实例化的系统。类型作为泛型类的类型参数