RelativeSource vs. ElementName
These two approaches can achieve the same result,
RelativeSource
Binding="{Binding Path=DataContext.MyBindingProperty,
RelativeSource={RelativeSource AncestorType={x:Type Window}}}"
This method looks for a control of a type Window (in this example) in the visual tree and when it finds it you basically can access it's DataContext
using the Path=DataContext....
. The Pros about this method is that you don't need to be tied to a name and it's kind of dynamic, however, changes made to your visual tree can affect this method and possibly break it.
ElementName
Binding="{Binding Path=DataContext.MyBindingProperty, ElementName=MyMainWindow}
This method referes to a solid static Name
so as long as your scope can see it, you're fine.You should be sticking to your naming convention not to break this method of course.The approach is qute simple and all you need is to specify a Name="..."
for your Window/UserControl.
Although all three types (RelativeSource, Source, ElementName
) are capable of doing the same thing, but according to the following MSDN article, each one better be used in their own area of specialty.
How to: Specify the Binding Source
Find the brief description of each plus a link to a more details one in the table on the bottom of the page.