本文介绍了如何使用WPF绑定与RelativeSource?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使用WPF绑定的 RelativeSource
和不同的用例?
How do I use RelativeSource
with WPF bindings and what are the different use-cases?
推荐答案
如果要绑定对象上的另一个属性:
If you want to bind to another property on the object:
{Binding Path=PathToProperty, RelativeSource={RelativeSource Self}}
如果要在祖代上获取属性:
If you want to get a property on an ancestor:
{Binding Path=PathToProperty,
RelativeSource={RelativeSource AncestorType={x:Type typeOfAncestor}}}
如果要在模板化父项上获取属性(因此可以在ControlTemplate中进行2种方式绑定)
If you want to get a property on the templated parent (so you can do 2 way bindings in a ControlTemplate)
{Binding Path=PathToProperty, RelativeSource={RelativeSource TemplatedParent}}
或更短(仅适用于OneWay绑定):
or, shorter (this only works for OneWay bindings):
{TemplateBinding Path=PathToProperty}
这篇关于如何使用WPF绑定与RelativeSource?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!