WPF Datagrid - deselect selected item(s) when clicking whitespace in the DataGrid
Asked Answered
D

6

15

The default behavior is to use CTRL+Click to deselect items in the Datagrid

I want to be able to mouse click (left or right button) the whitespace in the grid and have it deselect any selected items.

I've googled it to death and found some incredibly complex workarounds, but i'm hoping for a simple solution.

Edit:

I'm now using a listview instead, and still havent found a solution. It's slightly less annoying with a listview though because they are styled better.

Delamination answered 19/5, 2012 at 16:32 Comment(0)
M
18

I had the same question and found a solution. This should be built in behaviour:

private void dataGrid1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    if (sender != null)
    {
        DataGrid grid = sender as DataGrid;
        if (grid != null && grid.SelectedItems != null && grid.SelectedItems.Count == 1)
        {
            DataGridRow dgr = grid.ItemContainerGenerator.ContainerFromItem(grid.SelectedItem) as DataGridRow;
            if (!dgr.IsMouseOver)
            {
                (dgr as DataGridRow).IsSelected = false;
            }
         }
    }        
}
Mansur answered 5/8, 2013 at 20:4 Comment(3)
I know it's year old but still, shouldn't it be "if(dgr.IsMouseOver)" instead of "if(!dgr.IsMouseOver)"? It worked for me after that change.Ellora
Would like to point out, after a few years, that one may call grid.CommitEdit() to commit edits after leaving the cell currently being edited (if there are any). This may be helpful especially in the case of ComboBox cells.Constrain
I found this solution useful, even though it is a few years old. I would like to point out that the if(){ ... } statement can be simplified to dgr.IsSelected = dgr.IsMouseOver; since the check and assignment are both of type bool.Hennery
B
3

A simple

<DataGrid MouseDown="DataGrid_MouseDown">

is not what you want?

private void DataGrid_MouseDown(object sender, MouseButtonEventArgs e)
{
    (sender as DataGrid).SelectedItem = null;
}

The only disadvantage is that a click without CTRL on a selected item deselects all too.

Bright answered 20/5, 2012 at 2:23 Comment(0)
C
0

I am not sure whether you mean white space or gray space. In the latter case the following does the job:

    private void dataViewImages_MouseUp(object sender, MouseEventArgs e)
    {
        DataGridView.HitTestInfo hit = dataViewImages.HitTest(e.X, e.Y);
        if (hit.Type != DataGridViewHitTestType.Cell)
           dataViewImages.ClearSelection();
    }

This is what I use to deselect all cells by clicking in the gray space.

Ciccia answered 21/9, 2012 at 19:20 Comment(2)
Your answer is for WinForms, the question is about WPF.Vanesavanessa
Your answer isn't good for the question, as Matthew said, but you helped me solve the same problem in WinForms. So, thanks!Smarm
T
0
private void dg_IsKeyboardFocusWithinChanged
    (object sender, DependencyPropertyChangedEventArgs e)
    {
        if (dg.SelectedItem != null) {
            dg.UnselectAll();
        }
    }
Tricorn answered 8/7, 2014 at 12:35 Comment(0)
T
0

If you have SelectionUnit="FullRow" you have to use UnselectAllCells() instead UnselectAll().

Trierarch answered 16/3, 2017 at 12:33 Comment(0)
W
0

This also works with SelectionMode set to Extended:

private void DataGrid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    if ((sender is DataGrid grid) && (grid.SelectedCells != null) && (grid.SelectedCells.Count >= 1))
    {
        DataGridRow? selectedRow = null;
        foreach (object item in grid.SelectedItems)
        {
            DataGridRow dgr = (DataGridRow)grid.ItemContainerGenerator.ContainerFromItem(item);
            if (dgr.IsMouseOver)
            {
                selectedRow = dgr;
            }
        }

        grid.UnselectAllCells();

        if (selectedRow != null)
        {
            selectedRow.IsSelected = true;
        }
    }
}
Withershins answered 6/5 at 17:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.