Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In WPF, what is a TemplatedParent?

Tags:

wpf

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

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

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

Simply, what is a TemplatedParent please?

like image 865
David Smith Avatar asked Mar 14 '11 04:03

David Smith


People also ask

What is TemplatedParent WPF?

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. As a result, inside the template, if you want to refer to the new object, you use TemplatedParent for quick access.

What is TemplateBinding?

A TemplateBinding is an optimized form of a Binding for template scenarios, analogous to a Binding constructed with {Binding RelativeSource={RelativeSource TemplatedParent}, Mode=OneWay} . A TemplateBinding is always a one-way binding, even if properties involved default to two-way binding.

What is WPF RelativeSource?

WPF RelativeSource is a Markup Extension that assists us in binding data of an element with another source element using its relationship. It states about the source position that where it falls relative to a given element.


2 Answers

TemplatedParent refers to the Control instance that the template is being applied to. You can use TemplatedParent to bind to properties declared on that object (and its base classes).

like image 155
Gabe Avatar answered Sep 25 '22 19:09

Gabe


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.

As a result, inside the template, if you want to refer to the new object, you use TemplatedParent for quick access. Otherwise, you'll have to use RelativeSource to walk the tree upwards to find the parent object.

like image 31
Stephen Chung Avatar answered Sep 22 '22 19:09

Stephen Chung