AvaloniaUI / Avalonia.Controls.TreeDataGrid

A combined TreeView/DataGrid for Avalonia.
MIT License
266 stars 54 forks source link

Selection is lost when ContextMenu is displayed #161

Closed danipen closed 1 year ago

danipen commented 1 year ago

We are displaying the context menu using Control.ContextRequestedEvent. Previously, is worked as expected, but since we migrated to preview 6, we're losing the selection when the context menu is being displayed:

rightclick

grokys commented 1 year ago

How are you assigning the context menu? I'm not seeing this behavior in the sample app:

https://user-images.githubusercontent.com/1775141/231992381-8a61e600-7c2e-40a4-bec2-741bcf88e9bd.mp4

danipen commented 1 year ago

We're using:

mTreeDataGrid.ContextRequested += TreeDataGridEventHandlers.ContextRequested;

And then:

void ContextRequested(object sender, ContextRequestedEventArgs e)
{
    if (IsClickedOnHeader(e))
        return;

    e.TryGetPosition(targetControl, out Point p);

    mMenu.Popup(targetControl, p);
}
grokys commented 1 year ago

Just tried this, and though I'm not sure exactly what you're doing (our ContextMenu class doesn't have a Popup method, not MenuFlyout), I could reproduce with a MenuFlyout. The fix was simple though: add e.Handled = true to the ContextRequested event handler. Does this fix the issue for you?

danipen commented 1 year ago

Yes, setting e.Handled = true fixes the issue. Just note that in 0.10.x this was not needed ...

danipen commented 1 year ago

Also, for ListBox we're using the same approach and e.Handled = true is not needed either.

danipen commented 1 year ago

The workaround is ok for us, since we manage the ContextRequested event in a single place, so it's easy to workaround. But you may want to review the behavior since it's different for the TreeDataGrid and other lists, such as the ListBox... Please let me know.

grokys commented 1 year ago

The problem comes from this line:

https://github.com/AvaloniaUI/Avalonia.Controls.TreeDataGrid/blob/stable/0.10.x/src/Avalonia.Controls.TreeDataGrid/Selection/TreeDataGridRowSelectionModel.cs#L392

When the pointer is released, IsRightButtonPressed is false as the right button has been released. Will fix this.

note that in 0.10.x this was not needed

Strange, I tried the stable 0.10.x branch of TreeDataGrid and I see the same behavior there; not sure what could have changed between 0.10.x and 11.x to cause a difference in behavior, but it's academic because the code has a bug.

grokys commented 1 year ago

Should be fixed by https://github.com/AvaloniaUI/Avalonia.Controls.TreeDataGrid/pull/168

maxkatz6 commented 1 year ago

As a one who added ContextRequested event, you are supposed to handle this event, if you have opened any context menu. Otherwise, the same event can be handled by parent control and second menu might be opened. Even if this specific issue can be avoided in TDG, it still should be handled.