mHelpMe
mHelpMe

Reputation: 6668

binding datagrid column to selecteditem property

I have a datagrid where if a row is clicked upon the row details is shown. In the row details is another datagrid called dgRights.

So dgRights is bound to SelectItem.Funds where funds is a custom list. dgRights show 4 columns, 3 of which are bound fine however the fourth isn't (in my code below its called "Rights Sedol").

I want the column Rights Sedol to be bound to a property of the selectedItem not selectedItem.Funds, is this possible?

I've tried the lines of code below without luck,

<DataGridTextColumn Header="Rights Sedol" Binding="{Binding SelectedItem.NewSecurity.Sedol, RelativeSource={RelativeSource AncestorType=Window}}/>                                

2nd

<DataGridTextColumn Header="Rights Sedol" Binding="{Binding SelectedItem.NewSecurity.Sedol, RelativeSource={RelativeSource AncestorType=DataGrid}}/>   

I also tried changing the ItemSource of the datagrid from SelectedItem.Funds to just SelectItem & changed the other 3 working columns to Funds.Code etc but this didn't display any data in the datagrid at all. So not sure what to do?

App.xaml - my row details data template

<DataTemplate x:Key="DG_RowDetailRGHTSHist">
        <Grid x:Name="RowDetailGrid"            
              Margin="5"
              HorizontalAlignment="Left">
            <Border HorizontalAlignment="Left"
                    VerticalAlignment="Top"
                    Height="250"
                    CornerRadius="5">
                <Border.Background>
                    <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
                        <GradientStop Offset="0" Color="Transparent"/>
                        <GradientStop Offset="1" Color="Transparent"/>
                    </LinearGradientBrush>
                </Border.Background>
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="*"/>
                        <RowDefinition Height="4*"/>
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="400"/>
                        <ColumnDefinition Width="200"/>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>
                    <TextBlock Grid.Row="0"
                               Grid.Column="0"
                               Margin="5,5,5,5"
                               HorizontalAlignment="Left"
                               FontSize="12"
                               FontWeight="Bold"
                               Foreground="Black" 
                               Text="Fund Summary">
                    </TextBlock>
                    <DataGrid Grid.Row="1" Grid.Column="0"  Grid.ColumnSpan="2"
                              ItemsSource="{Binding SelectedItem.Funds,  RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"                                                                  
                              RowStyle="{StaticResource DG_Row}"  
                              ColumnHeaderStyle="{StaticResource DG_ColumnHeader}" 
                              RowHeaderStyle="{StaticResource DG_RowHeaderNested}"
                              CellStyle="{StaticResource DG_Cell}" 
                              Background="Silver"
                              HorizontalGridLinesBrush="LightGray"
                              VerticalGridLinesBrush="LightGray"
                              CanUserAddRows="False"
                              CanUserDeleteRows="False"
                              Margin="50,5,5,20"
                              AutoGenerateColumns="False">
                        <DataGrid.Columns>
                            <DataGridTextColumn Header="Fund Code" Binding="{Binding Code}" IsReadOnly="True" MinWidth="75"/>
                            <DataGridTextColumn Header="Fund Code SS" Binding="{Binding CodeSS}" IsReadOnly="True" MinWidth="75"/>
                            <DataGridTextColumn Header="Rights Sedol" Binding="{Binding SelectedItem.NewSecurity.Sedol, RelativeSource={RelativeSource AncestorType=Window}}" IsReadOnly="True" MinWidth="75"/>                                
                            <DataGridTextColumn Header="Number of Rights" Binding="{Binding CurrentNominal, Mode=TwoWay, StringFormat={}{0:N0}}" IsReadOnly="True"/>
                        </DataGrid.Columns>
                    </DataGrid>
                </Grid>
            </Border>
        </Grid>
    </DataTemplate>

Upvotes: 1

Views: 90

Answers (1)

J.H.
J.H.

Reputation: 4322

You want to access the 2nd datagrid up so you need to specify AncestorLevel=2.

{RelativeSource AncestorType=DataGrid, AncestorLevel=2}}

Upvotes: 2

Related Questions