Ctrl-click on a selected cell puts it in edition mode, while it should deselect it

1 Answer 140 Views
GridView
Eldoir
Top achievements
Rank 2
Iron
Iron
Iron
Eldoir asked on 07 Nov 2022, 10:32 AM | edited on 07 Nov 2022, 11:11 AM

Hello, I'm having this simple issue, I said it all in the title.
I'm using the RadGridView with SelectionMode="Extended" and SelectionUnit="Cell".
I've attached a sample project that reproduces the issue.

Repro steps:

  • Select cell "Manchester"
  • Ctrl-click again on cell "Manchester"
  • It puts it in edition mode instead of deselecting it

I've found a workaround where I ctrl-click on another cell ("Cell B"), then ctrl-click on the initial cell ("Cell A"), then ctrl-click on "Cell B" again, so that both cells are deselected. From what I understand, this is because the behavior is different whether I ctrl-click on the last selected cell, or on any other selected cell.

How can I fix this? Thanks! :)

Eldoir
Top achievements
Rank 2
Iron
Iron
Iron
commented on 07 Nov 2022, 03:10 PM

At the moment, my best lead is to subscribe to the BeginningEdit of the RadGridView and do this:

private void OnBeginningEdit(object sender, GridViewBeginningEditRoutedEventArgs e)
{
    if (KeyboardModifiers.IsControlDown)
    {
        AssociatedObject.SelectedCells.Remove(AssociatedObject.SelectedCells.Last());
        e.Cancel = true;
    }
}

This way, the cell does not enter edit mode, and it is deselected.
But I don't understand why it is still visually selected. Maybe some call that I'm missing to refresh the UI?

1 Answer, 1 is accepted

Sort by
0
Dilyan Traykov
Telerik team
answered on 09 Nov 2022, 03:22 PM

Hi Eldoir,

Thank you for the provided sample project and detailed explanation.

The reason for this behavior is the default value of the EditTriggers property of the RadGridView control - it is CurrentCellClick | F2 | TextInput.

This means that a cell will enter edit mode if it is already the current cell, when the user presses the F2 key or when they type in some text after first selecting the cell.

With the described action steps, the second click of the Manchester cell is a click on the current cell of the control which expectedly puts it into edit mode.

Having this in mind, one approach I can suggest for removing this undesired behavior would be to exclude the CurrentCellClick from the EditTriggers by setting the value to F2 | TextInput:

<telerik:RadGridView EditTriggers="F2,TextInput" ...

If this does not work for you, you can proceed with the approach you've already come up with by handling the BeginningEdit event. However, you need to delay the deselection of the cell as otherwise, it occurs too early to be detected by the control:

        private void OnBeginningEdit(object sender, GridViewBeginningEditRoutedEventArgs e)
        {
            if (KeyboardModifiers.IsControlDown)
            {
                Dispatcher.BeginInvoke(new Action(() =>
                {
                    AssociatedObject.SelectedCells.Remove(AssociatedObject.SelectedCells.Last());
                }));
                e.Cancel = true;
            }
        }

Please give this a try and let me know if any of these approaches works for you.

Regards,
Dilyan Traykov
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Eldoir
Top achievements
Rank 2
Iron
Iron
Iron
commented on 09 Nov 2022, 04:26 PM

Hi Dilyan,

I can't change the EditTriggers because we do want to enter edit mode on CurrentCellClick, just not when Ctrl is pressed.

The second approach you suggested works well in that the cell is now visually deselected!

But there is still one issue: the cell has this "in-between" state where it can still enter edit mode when pressing F2:

(cell 76212 on the screen below)
What can I do to avoid that? Basically, what I want when Ctrl-clicking on cell 76212, is that only Manchester cell is selected, and when I press F2, it should be Manchester cell that enters edit mode.

Thanks for you reply!

Dilyan Traykov
Telerik team
commented on 14 Nov 2022, 02:24 PM

Hi Eldoir,

I'm glad that this second approach would work for you.

As for the scenario that you described, one idea that comes to mind is to preserve the CurrentCell in the PreviewMouseDown event if the Control key is down and set the CurrentCellInfo property back to this cell upon MouseUp:

        private GridViewCell currentCell;

        private void Cell_MouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (KeyboardModifiers.IsControlDown)
            {
                this.gridView.CurrentCellInfo = new GridViewCellInfo(this.currentCell);
            }
        }

        private void Cell_PreviewMouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (KeyboardModifiers.IsControlDown)
            {
                this.currentCell = this.gridView.CurrentCell;
            }
        }

For your convenience, I've updated the sample project to demonstrate this. Please have a look and let me know if this provides the expected functionality.

Eldoir
Top achievements
Rank 2
Iron
Iron
Iron
commented on 14 Nov 2022, 05:27 PM

Hi Dilyan,
Thanks for the provided solution but unfortunately it is not satisfying for 2 reasons:

- There is a slight "blinking" effect on the cell borders, when ctrl-clicking a new cell
- If I select cell A, then ctrl-click on cell B to add it to the selection, then ctrl-click on cell A, both cells are deselected!

Dilyan Traykov
Telerik team
commented on 16 Nov 2022, 04:13 PM

Hi Eldoir,

I will try to address both issues you described:

- I'm afraid I cannot think of a straightforward way to avoid this change in the current cell border as the CurrentCellChanged event cannot be canceled and thus we need to change the current cell only after it has already been set. I do hope, however, that you can work with this small visual glitch as long as the rest of the functionality works as expected.

- I've updated the sample project to achieve what I believe is the desired behavior. I've removed the call to the Remove method of the SelectedCells collection which no longer seems to be required and this seems to provide the expected result.

Please have a look at the updated project and let me know if I'm missing something.

Tags
GridView
Asked by
Eldoir
Top achievements
Rank 2
Iron
Iron
Iron
Answers by
Dilyan Traykov
Telerik team
Share this question
or