WPF Datagrid Multiple Selection without CTRL or Space

I was creating an application with a similar requirement that would work for both touchscreen and desktop. After spending some time on it, the solution I came up with seems cleaner. In the designer, I added the following event setters to the datagrid:

<DataGrid.RowStyle>
   <Style TargetType="DataGridRow" >
     <EventSetter Event="MouseEnter" Handler="MouseEnterHandler"></EventSetter>
     <EventSetter Event="PreviewMouseDown" Handler="PreviewMouseDownHandler"></EventSetter>
   </Style>
</DataGrid.RowStyle>

Then in the codebehind, I handled the events as:

private void MouseEnterHandler(object sender, MouseEventArgs e)
{
    if (e.LeftButton == MouseButtonState.Pressed &&
        e.OriginalSource is DataGridRow row)
    {
        row.IsSelected = !row.IsSelected;
        e.Handled = true;
    }
}

private void PreviewMouseDownHandler(object sender, MouseButtonEventArgs e)
{
    if (e.LeftButton == MouseButtonState.Pressed && 
        e.OriginalSource is FrameworkElement element &&
        GetVisualParentOfType<DataGridRow>(element) is DataGridRow row)
    {
        row.IsSelected = !row.IsSelected;
        e.Handled = true;
    }
}

private static DependencyObject GetVisualParentOfType<T>(DependencyObject startObject)
{
    DependencyObject parent = startObject;

    while (IsNotNullAndNotOfType<T>(parent))
    {
        parent = VisualTreeHelper.GetParent(parent);
    }

    return parent is T ? parent : throw new Exception($"Parent of type {typeof(T)} could not be found");
}

private static bool IsNotNullAndNotOfType<T>(DependencyObject obj)
{
    return obj != null && !(obj is T);
}

Hope it helps somebody else too.


You can try this simple workaround without having to modifying/inheriting DataGrid control by handling preview mouse down event as follows:

TheDataGrid.PreviewMouseLeftButtonDown += 
                 new MouseButtonEventHandler(TheDataGrid_PreviewMouseLeftButtonDown);


void TheDataGrid_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    // get the DataGridRow at the clicked point
    var o = TryFindFromPoint<DataGridRow>(TheDataGrid, e.GetPosition(TheDataGrid));
    // only handle this when Ctrl or Shift not pressed 
    ModifierKeys mods = Keyboard.PrimaryDevice.Modifiers;
    if (o != null && ((int)(mods & ModifierKeys.Control) == 0 &&
                                                (int)(mods & ModifierKeys.Shift) == 0))
    {
        o.IsSelected = !o.IsSelected;
        e.Handled = true;
    }
}

public static T TryFindFromPoint<T>(UIElement reference, Point point)
                where T:DependencyObject
{
    DependencyObject element = reference.InputHitTest(point) as DependencyObject;
    if (element == null) 
        return null;
    else if (element is T) 
        return (T)element;
    else return TryFindParent<T>(element);
}

The TryFindFromPoint method, from a blog post by Philipp Sumi, is used to get the DataGridRow instance from point you clicked.

By checking ModifierKeys, you can still keep Ctrl and Shift as default behavior.

Only one draw back from this method is that you can't click and drag to perform range select like it can originally.


This is not supported in the DataGrid in the toolkit, and it looks like it won't be supported when the DataGrid is shipped with .NET 4 either. Yet another reason why this control is not ready for production use. I would go with one of these options:

  1. Roll your own grid with ListView/GridView
  2. Modify the source code of the DataGrid in the toolkit (it shouldn't be too hard since extended selection is already supported?)
  3. Look for any of the commercial WPF DataGrids available (they generally add huge amount of useful functionality)

I agree that the DataGrid should support this and I think you should file a bug/suggestion for this anyway. Maybe it's not too late to get it into .NET 4.. :)