Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DataGridColumnEventArgs doesn't contain keyboard modifiers from the column click event. #17976

Open
zv-gh opened this issue Jan 15, 2025 · 3 comments

Comments

@zv-gh
Copy link

zv-gh commented Jan 15, 2025

Describe the bug

The DataGridColumnHeader's ProcessSort method's default handling is using keyboard modifers (shift) to determine if it needs to do multi sort or not. However it doesn't forward the keyboard modifiers to the event handler. So in a user defined sort handler it is effectively impossible to replicate similar behaviour to the default handler. I have quoted the relevant datagrid source code below.

  • Since the Input manager and Keyboard Device seems to be internal I can't seem to do something like GetAsyncKeyState in windows API. I can't find an easy workaround for this either.
  • Basically if one would need the modifiers in a non-input event and the modifiers are not forwarded we are out of luck.

A potential solution for this one case is to include modifiers in DataGridColumnEventArgs.

        {
            // if we can sort:
            //  - AllowUserToSortColumns and CanSort are true, and
            //  - OwningColumn is bound
            // then try to sort
            if (OwningColumn != null
                && OwningGrid != null
                && OwningGrid.EditingRow == null
                && OwningColumn != OwningGrid.ColumnsInternal.FillerColumn
                && OwningGrid.CanUserSortColumns
                && OwningColumn.CanUserSort)
            {
                var ea = new DataGridColumnEventArgs(OwningColumn);
                OwningGrid.OnColumnSorting(ea);

                if (!ea.Handled && OwningGrid.DataConnection.AllowSort && OwningGrid.DataConnection.SortDescriptions != null)
                {
                    // - DataConnection.AllowSort is true, and
                    // - SortDescriptionsCollection exists, and
                    // - the column's data type is comparable

                    DataGrid owningGrid = OwningGrid;
                    DataGridSortDescription newSort;

                    KeyboardHelper.GetMetaKeyState(this, keyModifiers, out bool ctrl, out bool shift);

Thanks in advance!

To Reproduce

Try to access the shift key state from a custom sort handler and implement multi-sort.
<DataGrid Sorting="Example_Sorting">

Expected behavior

Keyboard modifiers are accessible in the Sort event.

Avalonia version

11.2.3

OS

No response

Additional context

No response

@zv-gh zv-gh added the bug label Jan 15, 2025
@timunie
Copy link
Contributor

timunie commented Jan 15, 2025

I think this would be a breaking change and could only (if even) considerd for v12.0

What you could do is listen for pointerpressed and released events and set a flag inside. You may have to tunnel the event.

@zv-gh
Copy link
Author

zv-gh commented Jan 16, 2025

Hi @timunie

I have an ad hoc fix locally that adds an extra modifier property to DataGridColumnEventArgs. That seems to work without breaking compatibility. There are places where the modifer is not accessible or relevant when using this argument type, in this case it is set to None.
But I also understand if you'd rather spin off a more specific event arg type specific to sort instead although it'd really break compatibility.

Thanks!
Image

@timunie
Copy link
Contributor

timunie commented Jan 16, 2025

If you think this works without breaking, feel free to provide a PR for a review / API discussion. Maybe adding a property to an existing event arg is allowed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants