The default behaviour of a WPF DataGrid
is to select when a row is clicked if SelectionMode="Extended"
which is what I want, however I also wish for the row to un-select if it was previously already selected when clicked.
I have tried the following which will unselect the row as soon as it's selected, it seems the row selection occurs before the mouse click event.
private void DoGridMouseLeftButtonUp(object sender, MouseButtonEventArgs args) {
// Get source row.
DependencyObject source = (DependencyObject)args.OriginalSource;
var row = source.FindParent<DataGridRow>();
if (row == null)
return;
// If selected, unselect.
if (row.IsSelected) {
row.IsSelected = false;
args.Handled = true;
}
}
Where I am binding to this event with the following grid.
<DataGrid SelectionMode="Extended"
SelectionUnit="FullRow"
MouseLeftButtonUp="DoGridMouseLeftButtonUp">