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

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

Open
sinatrocious opened this issue Mar 13, 2024 · 1 comment

Comments

@sinatrocious
Copy link

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
Copy link
Author

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;
    }
}

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

No branches or pull requests

1 participant