How do I use WPF bindings with RelativeSource?

David Schmitt picture David Schmitt · Sep 17, 2008 · Viewed 442.9k times · Source

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

Answer

Abe Heidebrecht picture Abe Heidebrecht · Sep 17, 2008

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}