如何将 WPF 绑定与 RelativeSource 结合使用? [英] How do I use WPF bindings with RelativeSource?

查看:29
本文介绍了如何将 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}}}

如果你想在模板化的父级上获得一个属性(所以你可以在 ControlTemplate 中进行两种方式的绑定)

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 结合使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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