在 WPF 中,什么是 TemplatedParent?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5294611/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 22:39:46  来源:igfitidea点击:

In WPF, what is a TemplatedParent?

wpf

提问by David Smith

I'm trying to understand RelativeSource binding but I can't find a definition for TemplatedParent. MSDN says:

我正在尝试了解 RelativeSource 绑定,但找不到 TemplatedParent 的定义。 MSDN 说:

"Gets a static value that is used to return a RelativeSource constructed for the TemplatedParent mode."

“获取一个静态值,用于返回为 TemplatedParent 模式构造的 RelativeSource。”

It doesn't appear in the index of "Windows Presentation Foundation Unleashed".

它没有出现在“Windows Presentation Foundation Unleashed”的索引中。

Simply, what is a TemplatedParent please?

简单地说,什么是 TemplatedParent?

回答by Gabe

TemplatedParentrefers to the Controlinstance that the template is being applied to. You can use TemplatedParentto bind to properties declared on that object (and its base classes).

TemplatedParentControl应用模板的实例。您可以使用TemplatedParent绑定到在该对象(及其基类)上声明的属性。

回答by Stephen Chung

In WPF, a template is what it uses to construct the tree of a type. WPF essentially makes a copy of the template when a new object of that type is created.

在 WPF 中,模板是用来构造类型树的。WPF 本质上是在创建该类型的新对象时制作模板的副本。

As a result, insidethe template, if you want to refer to the new object, you use TemplatedParentfor quick access. Otherwise, you'll have to use RelativeSourceto walk the tree upwards to find the parent object.

因此,模板内部,如果要引用新对象,则使用TemplatedParent快速访问。否则,您将不得不使用RelativeSource向上走树来找到父对象。

回答by Bigeyes

Notice:

注意:

TemplatedParent means Parent is Templated. Templated is passive voice in English. In other words, the parent element is applied with a Template. So in the template you can binding the parent's property easily.

TemplatedParent 表示 Parent 是模板化的。Templated 是英语中的被动语态。换句话说,父元素与模板一起应用。因此在模板中您可以轻松绑定父级的属性。