Hi all this might sound trivial but I want to stop drag select on WPF
DataGrid
I have a simple grid like
<DataGrid ItemsSource="{Binding Items}" SelectionMode="Extended">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding .}"/>
</DataGrid.Columns>
</DataGrid>
how can I stop multiple selection on click drag but and have multiple selection by Shift and Ctrl.
thanks
Mikolaytis's answer is incomplete. With that solution, clicking an unselected row selects all rows between it and the first selected row above it, for as long as the mouse button is down. This is a side effect of DataGrid._isDraggingSelection still being true, which is evaluated in other mouse-event-driven operations.
Il Vic's answer, while I did not try it, is far more complex than necessary.
Instead, in the override for DataGrid.OnMouseMove() (as was done in Mikolaytis's answer), call private method DataGrid.EndDragging(), via reflection:
public class MyDataGrid : DataGrid
{
private static readonly FieldInfo s_isDraggingSelectionField =
typeof(DataGrid).GetField("_isDraggingSelection", BindingFlags.Instance | BindingFlags.NonPublic);
private static readonly MethodInfo s_endDraggingMethod =
typeof(DataGrid).GetMethod("EndDragging", BindingFlags.Instance | BindingFlags.NonPublic);
// DataGrid.OnMouseMove() serves no other purpose than to execute click-drag-selection.
// Bypass that, and stop 'is dragging selection' mode for DataGrid
protected override void OnMouseMove(MouseEventArgs e)
{
if ((bool)(s_isDraggingSelectionField?.GetValue(this) ?? false))
s_endDraggingMethod.Invoke(this, new object[0]);
}
}
Put simply, after drag-selection is started, the next mouse move event ends it.