Alex Rivera | Logout

How do I use WPF bindings with RelativeSource?

Asked 2008-09-17T15:10:53.583
673

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

Edit
Report

2 Answers

23

Don't forget TemplatedParent:

<Binding RelativeSource="{RelativeSource TemplatedParent}"/>

or

{Binding RelativeSource={RelativeSource TemplatedParent}}
answered 2008-09-17T15:14:34.120
16

Some useful bits and pieces:

Here's how to do it mostly in code:

Binding b = new Binding();
b.RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor, this.GetType(), 1);
b.Path = new PropertyPath("MyElementThatNeedsBinding");
MyLabel.SetBinding(ContentProperty, b);

I largely copied this from Binding Relative Source in code Behind.

Also, the MSDN page is pretty good as far as examples go: RelativeSource Class

answered 2012-10-30T11:22:06.790

Your Answer