Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use WPF bindings with RelativeSource?

How do I use RelativeSource with WPF bindings and what are the different use-cases?

like image 571
David Schmitt Avatar asked Sep 17 '08 15:09

David Schmitt


People also ask

What is WPF RelativeSource?

The RelativeSource is a markup extension that is used in particular binding cases when we try to bind a property of a given object to another property of the object itself, when we try to bind a property of a object to another one of its relative parents, when binding a dependency property value to a piece of XAML in ...

What is templated parent in 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 does binding DO WPF?

Data binding is a mechanism in WPF applications that provides a simple and easy way for Windows Runtime apps to display and interact with data. In this mechanism, the management of data is entirely separated from the way data. Data binding allows the flow of data between UI elements and data object on user interface.

How many types of binding are there in WPF?

WPF binding offers four types of Binding.


1 Answers

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} 
like image 200
Abe Heidebrecht Avatar answered Sep 24 '22 17:09

Abe Heidebrecht