EmanueleVerderame
EmanueleVerderame

Reputation: 1

How to disable unselection in datagrid

I have a datagrid with SelectionMode = "single" , but when i press 'ctrl' and select the selected row , it became unselected. There is a way to disable the 'ctrl' command or make it that when is pressed it doesn't unselect the selected row?

I am using WPF and MVVM Pattern.

I tried with

PreviewMouseLeftButtonDown="DataGrid_PreviewMouseLeftButtonDown"

and handled it like this but doesn't worked :

private void DataGrid_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {

 e.handled = true;

    }

`

Upvotes: -1

Views: 142

Answers (1)

Blacktempel
Blacktempel

Reputation: 3995

The answer on here suggests that there's more than just setting Handled to true. Also this might be helpful.

Both combined the result could look like this:

void DataGridPreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    e.Handled = true;

    var result = VisualTreeHelper.HitTest(gd, e.GetPosition(gd));
    var row = DependencyObjectHelper.FindAncestor<DataGridRow>(result.VisualHit);
    if (row != null && !row.IsSelected)
        row.IsSelected = true;
}

I suggest you take a closer look into existing questions & answers and combine these to accomplish what you want to do.

Upvotes: 1

Related Questions