How do I use RelativeSource
with WPF bindings and what are the different use-cases?
| |||
|
If you want to bind to another property on the object:
If you want to get a property on an ancestor:
If you want to get a property on the templated parent (so you can do 2 way bindings in a ControlTemplate)
or, shorter (this only works for OneWay bindings):
| |||||||||
|
The default attribute of
| |||||
|
Here is the cheat-sheet that does help not being confused: http://www.nbdtech.com/Free/WpfBinding.pdf -aj | |||||
|
Don't forget TemplatedParent:
or
| |||
|
It's worthy of note that for those stumbling across this thinking of Silverlight: Silverlight offers a reduced subset only, of these commands | |||
|
I just posted another solution for accessing the DataContext of a parent element in Silverlight that works for me. It uses | |||
|
Here's a more visual explanation in the context of a MVVM architecture: (http://www.rootsilver.com/2009/05/wpf-relativesource-ancestor-datacontext) |