The items are not sorted after setting column SortDirection, despite the column header shows arrow.
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
- Create new avalonia desktop application.
- Add
Avalonia.Controls.TreeDataGridnuget. - 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>
- Run, click button several time and observe issue:
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.
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;
}
}