views:

53

answers:

2

Hey all,

I'm kind of new to WPF, and I'm trying to do some specialized data binding. Specifically, I have a DataGrid that is bound to a collection of objects, but then I want the headers of the columns to be bound to a separate object. How do you do this?

I have a couple of classes defined like so:

public class CurrencyManager : INotifyPropertyChanged
    {
        private string primaryCurrencyName;

        private List<OtherCurrency> otherCurrencies;

         //I left out the Properties that expose the above 2 fields- they are the standard
        //I also left out the implementation of INotifyPropertyChanged for brevity
}

public class OtherCurrency : INotifyPropertyChanged
    {
        private string name;
        private double baseCurAmt;  
        private double thisCurAmt;

        //I left out the Properties that expose the above 3 fields- they are the standard
        //I also left out the implementation of INotifyPropertyChanged for brevity
}

Then the important section of XAML is as follows. Assume that I've already bound the Page to a specific object of type CurrencyManager. Note how binding attached to the header of the 2nd DataGridTextColumn is incorrect, and needs to somehow get access to the CurrencyManager object's property PrimaryCurrencyName. That is, the header of the column has the name "PrimaryCurrencyName", and the data in the column is still bound to the property ThisCurAmt for each element of the OtherCurrencies List.

<DataGrid ItemsSource="{Binding Path=OtherCurrencies}"  AutoGenerateColumns="False" RowHeaderWidth="0">
                    <DataGrid.Columns>
                        <DataGridTextColumn Header="Currency Name" Binding="{Binding Path=Name}"/>
                        <DataGridTextColumn Binding="{Binding Path=BaseCurAmt}">
                            <DataGridTextColumn.Header>
                                <Binding Path="PrimaryCurrencyName"/> 
                            </DataGridTextColumn.Header>
                        </DataGridTextColumn>
                        <DataGridTextColumn Header="Amt in this Currency" Binding="{Binding Path=ThisCurAmt}"/>
                    </DataGrid.Columns>

                </DataGrid>

How do I do this? Thanks!

A: 

The problem is, that the DataGridTextColumn is not a part of the visual tree.

Normaly, this can be be worked around using DataGridTemplateColumn but in your case, I think this will not help.

Probably this article from Jaime Rodriguez will lead you to a solution (I have only looked fast at it, but It looks appropriate).

HCL
After some work, I have successfully implemented the solution explained in the article above. Thanks, HCL!
skybluecodeflier
A: 

Try this one:

<DataGridTextColumn Binding="{Binding Path=BaseCurAmt}">
    <DataGridTextColumn.Header>
        <TextBlock>
            <TextBlock.Text>
                <Binding Path="DataContext.PrimaryCurrencyName" 
                        RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType={x:Type DataGrid}}"/>    
            </TextBlock.Text>
        </TextBlock>
    </DataGridTextColumn.Header>
</DataGridTextColumn>

Basically, this one uses a RelativeSource to find the DataGrid's DataContext (which I'm assuming is a CurrencyManager) and display its PrimaryCurrencyName property. Hope this helps.

karmicpuppet