I have two DataGrids and only want to have a selected row in one of the DataGrids at a time. when i make a selection in one DataGrid, the selected row in the other DataGrid should be removed. I have tried using the OnSelectionChanged even to change the selection in the opposing DataGrid, but this in turn caused the OnSelectionChanged even in the current DataGrid to be called, and i end up with no selection at all. Does anyone have any idea how to accomplish this?
<DataGrid x:Name="DataGrid1"
DockPanel.Dock="Top"
ItemsSource="{Binding DataGrid1CollectionView}"
SelectedItem="{Binding DataGrid1SelectedArisingGroup}"
SelectionChanged="DataGrid1SelectionChanged"
>
</DataGrid>
<DataGrid x:Name="DataGrid2"
DockPanel.Dock="Top"
ItemsSource="{Binding DataGrid2CollectionView}"
SelectedItem="{Binding DataGrid2SelectedArisingGroup}"
SelectionChanged="DataGrid2SelectionChanged"
>
</DataGrid>
private void DataGrid1SelectionChanged(object sender, SelectionChangedEventArgs e)
{
DataGrid2.SelectedItem=null;
}
private void DataGrid2SelectionChanged(object sender, SelectionChangedEventArgs e)
{
DataGrid1.SelectedItem=null;
}
You could unsubscribe/detach the SelectionChanged event of the second datagrid while a selection is done in the first datagrid & vice versa & then reattach after executing your logic - in this case unselecting the item.
I have written a code sample below for one of the datagrid's selection changed event. Extending it for the 2nd datagrid is pretty straight forward.
XAML:
<DataGrid x:name="dgr1" SelectionChanged="dgr1_Selection"/>
<DataGrid x:name="dgr2" SelectionChanged="dgr2_Selection"/>
Code Behind:
private void dgr1_Selection(object sender, SelectionChangedEventArgs e)
{
dgr2.SelectionChanged -= dgr2_Selection;
//unselecte the selected item of dgr2 - Set the IsSelected property of
//the Selected item to false or dgr2.SelectedItem=null
dgr2.SelectionChanged += dgr2_Selection;
}