Dyy
Dyy

Reputation: 61

Inner datagrid is firing the selection change event of the outer data grid C# WPF

I have a inner datagrid using row details template:

<DataGrid x:Name="OuterGrid" SelectionChanged="OuterGrid_SelectionChanged">
  <DataGrid.Columns>
    <DataGridTemplateColumn x:Name ="Header" Header="Header"     IsReadOnly="True" Width="480">
      <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
          <Expander x:Name="Expander" Grid.Row="0"     Expanded="Expander_Expanded" Collapsed="Expander_Collapsed">
            <Expander.Header>
              <TextBlock x:Name="TextBlockExp" Margin="15,0,0,0" Text="{Binding HeaderLabel}"/>
            </Expander.Header>
            <Grid>
            </Grid>
          </Expander>
          <DataTemplate.Triggers>
            <DataTrigger Binding="{Binding Path=IsSelected, RelativeSource=    {RelativeSource AncestorType=DataGridRow}}" Value="True">
              <Setter Property="Foreground" TargetName="TextBlockExp" Value="White"/>
            </DataTrigger>
          </DataTemplate.Triggers>
        </DataTemplate>
      </DataGridTemplateColumn.CellTemplate>
    </DataGridTemplateColumn>
    </DataGrid.Columns>

  <DataGrid.RowDetailsTemplate>
    <DataTemplate>
      <DataGrid x:Name="InnerGrid"  SelectionChanged="InnerGrid_SelectionChanged" ItemsSource="{Binding InnerGridList}" SelectedItem="{Binding SelectedSecondItem}">
        <DataGrid.Columns>
          <DataGridTextColumn Header="A" Binding="{Binding AValue}" IsReadOnly="True" Width="320">
          </DataGridTextColumn>
          <DataGridTextColumn Header="B" Binding="{Binding BValue}" IsReadOnly="True" Width="250">
          </DataGridTextColumn>
          <DataGridTextColumn Header="C" Binding="{Binding CValue}" IsReadOnly="True" Width="250">
          </DataGridTextColumn>
        </DataGrid.Columns>
      </DataGrid>
    </DataTemplate>
  </DataGrid.RowDetailsTemplate>
</DataGrid>

Binding of the outer grid and inner grid are already OK. However, when i try to change the selected item in inner grid, the selection change event of the outer grid is also called.

private void OuterGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
   if (OuterGrid.SelectedItems.Count > 0)
   {
                OuterGridModel row =     (OuterGridModel)OuterGrid.SelectedItems[0];
                PopulateInnerGrid(row.InnerGridList);
                row.SelectedSecondItem = row.InnerGridList[0];
   }
}

When I set the selected item using this line (as shown above):

row.SelectedSecondItem = row.InnerGridList[0];

InnerGrid_SelectionChanged is fired, also OuterGrid_SelectionChanged is fired. I only want InnerGrid_SelectionChanged.

Upvotes: 1

Views: 511

Answers (1)

SamTh3D3v
SamTh3D3v

Reputation: 9944

SelectionChanged is a routedevent and it's bubbling up in the element tree, both the inner and outer DataGrid listen to it. An easy fix to your issue is to set the Handled property of the SelectionChangedEventArgs to true, so that the event won't be handled any further.

 private void InnerGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
          //...
            e.Handled = true;
        }

Upvotes: 1

Related Questions