Avalonia.Controls.TreeDataGrid icon indicating copy to clipboard operation
Avalonia.Controls.TreeDataGrid copied to clipboard

The items are not sorted after setting column SortDirection, despite the column header shows arrow.

Open sinatrocious opened this issue 1 year ago • 2 comments

Describe the bug

I am setting column SortDirection in code, but the sorting is not happening. Sorting seems to only works if I click column header.

To Reproduce

  1. Create new avalonia desktop application.
  2. Add Avalonia.Controls.TreeDataGrid nuget.
  3. Do following changes :
Repro

App.axaml

    <Application.Styles>
        <FluentTheme />
        <StyleInclude Source="avares://Avalonia.Controls.TreeDataGrid/Themes/Fluent.axaml"/>
    </Application.Styles>

MainViewModel.cs

public class MainViewModel : ViewModelBase
{
    public AvaloniaList<Item> Items { get; } = new();
    public HierarchicalTreeDataGridSource<Item> Source { get; }

    public ReactiveCommand<Unit, Unit> Command { get; }

    public MainViewModel()
    {
        Source = new(Items)
        {
            Columns =
            {
                new HierarchicalExpanderColumn<Item>(new TextColumn<Item, int>("N", o => o.N), o => o.Chilren)
                {
                    SortDirection = ListSortDirection.Ascending
                }
            }
        };

        Command = ReactiveCommand.Create(() => Items.Add(new()));
    }
}

public class Item
{
    public int N { get; }
    public AvaloniaList<Item> Chilren { get; } = [];

    public Item() => N = Random.Shared.Next(10);
}

MainView.axaml

    <Grid>
        <TreeDataGrid Source="{Binding Source}"/>
        <Button Content="Add"
                Command="{Binding Command}"/>
    </Grid>
  1. Run, click button several time and observe issue:

TreeDataGrid SortDirection bug

Expected behavior

I expect sorting to work according to SortDirection.

Environment

  • OS: Windows 11
  • Avalonia-Version: 11.0.10
  • TreeDataGrid-Version: 11.0.2

Additional context

I've tried to set SortDirection after 5s delay after window is shown. The column header will show arrow, but no sorting occurs. I guess this public property is used internally to toggle displaying of arrow, but then how do I force sorting?

I guess simulating mouse click on column header is a workaround, but after quick research I am unable to find anything what can help me to build such workaround.

sinatrocious avatar Mar 13 '24 11:03 sinatrocious

Possible workaround is to send click event to first column:

var firstColumn = Children<TreeDataGridColumnHeader>(treeDataGrid).First();
firstColumn.RaiseEvent(new RoutedEventArgs { RoutedEvent = Button.ClickEvent });
Children method
IEnumerable<T> Children<T>(Visual parent)
{
    foreach (var child in parent.GetVisualChildren())
    {
        if (child is T target)
            yield return target;

        foreach (T item in Children<T>(child))
            yield return item;
    }
}

sinatrocious avatar Mar 13 '24 13:03 sinatrocious