如何使用WPF绑定使用的RelativeSource? [英] How do I use WPF bindings with RelativeSource?

查看:258
本文介绍了如何使用WPF绑定使用的RelativeSource?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何使用的RelativeSource 与WPF绑定,什么是不同的使用情况?

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}}}

如果你想获得对模板化父属性(这样你就可以在一个控件模板做双向绑定)

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}}

或短(这仅适用于单向绑定):

or, shorter (this only works for OneWay bindings):

{TemplateBinding Path=PathToProperty}

这篇关于如何使用WPF绑定使用的RelativeSource?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆