views:

70

answers:

1

Hello, I have a DataSet bound to the Window.DataContext; I also have a DataGrid

<DataGrid ItemsSource={Binding Tables[Items]}>
    <DataGrid.Columns>
        <DataGridTextBoxColumn Header={Binding Path=DataContext.Tables[Names]/Test, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}} />
    </DataGrid.Columns>
</DataGrid>

Basically, I'm trying to bind the Header of that column to DataTable "Names", Column "Test", first row.

However, I can't get it right. Note that I can bind it fine outside the DataGrid. The Grid's ItemsSource changes the data context and I don't know how to refer outside to the original DataContext.

It seems that the binding succeeds; but the problem is that the current item (first row) of the Tables[Names] in the Window.DataContext got lost.

If I make the DataSet static and access it via {x:Static local:dataset} then things work fine. But I can't use static datasets because there will be multiple instances (multi-user).

Can anyone please point me in the right direction?

A: 

Don't know if this will work for you situation, but you could try something like this: 1) Give your Window a Name attribute e.g. Name=ZeWindow. 2) Bind your DataGridTextBoxColumn Header like this:

<DataGridTextBoxColumn Header="{Binding Path=DataContext.Tables[Names]/Text, ElementName=ZeWindow}"/>

So basically, instead of binding to the DataContext of the DataGrid, you bind to the DataContext of the UIElement with Name=ZeWindow.

P.S.: I'm pretty new to WPF, so this might not work with the Window, but I did something similar using UserControls

Gilles Radrizzi
I also tried this. It gives the same result as the FindAncestor method in my post. Ie: it binds successfully, but the "CurrentItem" of the original DataContext got lost. The culprit of my problem is that I'm trying to refer to another DataTable that has its own CurrentItem (row[0]); if I refer to just a simple property then your method as well as the FindAncestor would work fine.
vuduy
Ok I see, then I'm sorry but I don't know what could be the problem. Hopefully someone else will know
Gilles Radrizzi