如何将RelativeSource
与WPF绑定一起使用?有哪些不同的用例?
发布于 2008-09-17 15:14:52
如果要绑定到对象上的另一个属性:
{Binding Path=PathToProperty, RelativeSource={RelativeSource Self}}
如果要获取祖先的属性,请执行以下操作:
{Binding Path=PathToProperty,
RelativeSource={RelativeSource AncestorType={x:Type typeOfAncestor}}}
如果你想在模板化的父元素上获取一个属性(这样你就可以在一个ControlTemplate中进行双向绑定)
{Binding Path=PathToProperty, RelativeSource={RelativeSource TemplatedParent}}
或者更短(这只适用于OneWay绑定):
{TemplateBinding Path=PathToProperty}
发布于 2009-03-03 09:24:23
Binding RelativeSource={
RelativeSource Mode=FindAncestor, AncestorType={x:Type ItemType}
}
...
RelativeSource
的默认属性是Mode
属性。这里给出了一组完整的有效值(from MSDN):
displayed.
,则使用此模式
发布于 2011-03-16 10:35:13
在MVVM架构的上下文中有一个更直观的解释:
https://stackoverflow.com/questions/84278
复制相似问题