Binding RelativeSource

Sep 18, 2009 at 4:36 PM


Is there a specific reason why the Binding to an element needs to be

DataContext="{Binding RelativeSource={RelativeSource Self}}

This works if I have a textblock within the main grid. However, if i have a textblock within a Template of a datagrid, it does not seem to bind.

On a similar note, is there also a way to Bind the ExpressionObserver over a PagedViewCollection or the DataContext of a control e.g. a Datagrid. I am trying to bind a datagrid to a PagedCollectionView which inturn is binded to an ObservableCollection. However, I need to track the current view shown by the PagedCollectionView (after a filter is applied / a different page is being viewed).

Thanks again for the help.




Sep 19, 2009 at 6:29 AM

Hi Vinay,

No, there is no special need for the DataContext to be set like that. It is just a way to bind to the desired values. Any valid binding will work. The obtics properties are just normal properties.

But that is all standard WPF / Silverlight stuff. I'd suggest grab a good WPF / Silverlight book because there are may options.



Sep 21, 2009 at 2:20 PM

In the sample you sent, if the DataContext is not set to RelativeSource Binding the information is not even displayed. I was nt sure if that was how it was intended. I was trying to implement the same on a Textblock included in a template (A Header template within a datagrid). However, in that case the binding does nt seem to pick up.


Sep 22, 2009 at 3:00 AM

Hi Vinay,

the Path in Bindings in WPF and Silverlight are searched from a sertain source. This source can be:

  1. Given explicitly using the Source attribute in the binding.
  2. Given explicitly using the ElementName attribute in the binding. This refers then to the object in the same namingscope as the element you set the binding on, with the specified name.
  3. Given relatively to the object you define the binding on using RelativeSource. (a bit limited in silverlight)
  4. Using the default which is the 'DataContext' of the element you set the binding on. This DataContext is passed from parent to child unless overriden on a given element.

In the example the DataContext was overriden on the top-level element (UserControl), after which it was passed on to all it's children. The TextBox was a child of this UserControl and therefore inherited this DataContext. As you noticed this DataContext was set to the UserControl itself. This makes sense because the properties we wanted to bind to are on the UserControl (code behind). The Path in the binding then simply needed to refer to the Sum property of the UserControl and then the Value.

Different way would have been to set a name on the UserControl and use ElementName in the binding.

<UserControl x:Class="Poc.MainPage"
    xmlns:d="" xmlns:mc="" 
    mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480"
  <Grid x:Name="LayoutRoot">
        <TextBox Text="{Binding ElementName=TopDog, Path=Sum.Value}" />

There are also some special things you can do in the path of a binding that has to do with the currently selected item in a view source. What way is best in your situation I don't know. Sometimes it can be quite tedious to get right. I do know that WPF/Silverlight programming is a lot about data binding. So my best advice is still to get a good book and study all the options and details.

Hope this helps and Regs,