1
votes

I'm having trouble with a datagrid and I'm wondering if I can make a binding work a certain way. If I can, it would save me from some heavy refactoring that wouldn't really be appropriate.

Anyway, in the below grid I have the main DataGrid columns/rows as well as a RowDetailsTemplate for the sub-rows. The main DataGrid is bound to the Positions collection and the RowDetailsTemplate is bound to the Taxlots collection. Items in the Taxlots collection do not have a Description or Ticker property, whereas items in the Positions collection do.

Is it possible to bind the Ticker and Description columns in the RowDetailsTemplate to the respective columns in the main DataGrid?

Simplified XAML below:

        <DataGrid x:Name="SecurityDataGrid"
                  ItemsSource="{Binding Positions,
                                NotifyOnSourceUpdated=True, 
                                UpdateSourceTrigger=PropertyChanged}">
            <DataGrid.RowDetailsTemplate>
                <DataTemplate>
                    <DataGrid ItemsSource="{Binding Taxlots}">
                        <DataGrid.Columns>
                            <DataGridTextColumn Binding="{Binding Ticker}"/>
                            <DataGridTextColumn Binding="{Binding Description}"/>
                            <DataGridTextColumn Binding="{Binding Shares}"/>
                        </DataGrid.Columns>
                    </DataGrid>
                </DataTemplate>
            </DataGrid.RowDetailsTemplate>
            <DataGrid.Columns>
                <DataGridTextColumn Width="Auto" Header="Ticker" Binding="{Binding Path=Ticker, Mode=OneWay}"
                                    TextOptions.TextFormattingMode="Display" />
                <DataGridTextColumn Width="Auto" Header="Description" Binding="{Binding Path=Description, Mode=OneWay}"
                                    TextOptions.TextFormattingMode="Ideal" />
                <DataGridTextColumn Width="Auto" Header="Shares" Binding="{Binding SharesOwned, Mode=OneWay, Converter={StaticResource DecimalToStringConverter}}"
                                    CellStyle="{StaticResource CellStyleRight}" />                    
            </DataGrid.Columns>
        </DataGrid>
1
Please see this, I think RelativeSource may work. - CodingYoshi
That did it perfectly. I wish I could give you the credit but I don't think comments allow for that. - CubemonkeyNYC
That's great! I hope you gave credit to that answer in the link by upvoting it. - CodingYoshi
I did now, thanks. - CubemonkeyNYC

1 Answers

0
votes

CodingYoshi nailed it. I suspected it would be a FindAncestor RelativeSource binding but I'm so new at XAML that I wasn't sure how to do it. I wound up using this:

Binding="{Binding RelativeSource={RelativeSource FindAncestor,
          AncestorType={x:Type DataGrid}},
          Path=DataContext.*PropertyName*}"