views:

1359

answers:

2

I have a grid of items which is populated using databinding. In the grid I have a DataTemplate for certain cells. I need to access the DataContext of the root element (the one which is hosting the grid) so that I can access additional bindings to support my datatemplate.

So you have:

Window
    Window.DataContext = TheDataSourceWithItemsAndSupports
    DataGrid.ItemsSource = {Binding Items}
        DataTemplate
            ListBox.ItemsSource = {Binding Supports}

I want the {Binding Supports} on TheDataSourceWithItemsAndSupports, but I don't see how to do that. I tried specifying {Binding} but that always returns null. I also tried using RelativeSource FindAncestor, but that yields null too.

Any clues?

+5  A: 

Maybe try

Window Name="TheWindow"
...
ListBox.ItemsSource = {Binding DataContext.Supports, ElementName=TheWindow}
I think the binding path would be DataContext.Supports, but this was my idea as well.
Robert Macnee
You're right, amended.
This worked, but I had to do some more coding (the scenario given above wasn't an exact representation of the situation at hand).
Inferis
A: 

It should work the way you describe. Only thing I see your DataTemplate is not ItemTemplate. You should also look at the output window to see where bindings fail.

Sergey Aldoukhov