What is the use of the RelativeSource property ?

Platform: WPF| Category: DataBinding

The ‘RelativeSource’ property of the Binding class is used to bind the data from an element by it’s relationship to the source element. RelativeSource is also a markup extension of type RelativeSource.

The following are some of the ways that RelativeSource can be used.

1. When the source element is equal to the target element,

[C#]

{Binding RelativeSource={RelativeSource Self}}

2. When the source element is equal to the target element’s TemplatedParent,

[C#]

{Binding RelativeSource={RelativeSource TemplatedParent}}

3. When the source element is equal to the closest parent of a given type,

[C#]

{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type StackPanel}}, Path=Orientation}

4. When the source element is equal to the nth closest parent of a given type,

[C#]

{Binding RelativeSource={RelativeSource FindAncestor,AncestorLevel=1, AncestorType={x:Type Window}}, Path=Title}

5. When the source element is equal to the previous data item in a data – bound collection.

[C#]

{Binding RelativeSource={RelativeSource PreviousData}}

Share with

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.