Datagrid ClearSort doesn't work if CanUserSortColumns subsequently synchronously set to false
See original GitHub issueDescribe the bug Consider the case where the developer wishes to sometimes let the user sort columns by clicking the header, and at other times disable manual sorting. The developer wants to write code to clear the user-provided sorts for all columns, then disable the user from providing further sorts. In this case, the idiomatic way is to iterate the columns, clearing the sort for each, then set the CanUserSortColumns to false. However, it doesn’t work quite right.
To Reproduce Steps to reproduce the behavior:
Front End
<Window xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="DataGridTest.MainWindow"
Title="DataGridTest">
<StackPanel>
<Button Content="Clear Sort" Click="OnClearSortClick"></Button>
<DataGrid x:Name="DataGrid" Items="{Binding Persons}">
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding Name}" />
<DataGridTextColumn Header="Age" Binding="{Binding Age}" />
</DataGrid.Columns>
</DataGrid>
</StackPanel>
</Window>
Code Behind
using Avalonia.Controls;
using Avalonia.Interactivity;
using System.Collections.ObjectModel;
namespace DataGridTest;
public partial class MainWindow : Window
{
public ObservableCollection<Person> Persons { get; } = new ObservableCollection<Person>
{
new Person { Name = "John Doe", Age = 30 },
new Person { Name = "Jane Doe", Age = 25 },
// ...
};
public MainWindow()
{
DataContext = this;
InitializeComponent();
}
private void OnClearSortClick(object sender, RoutedEventArgs e)
{
foreach (var column in DataGrid.Columns)
{
column.ClearSort();
}
DataGrid.CanUserSortColumns = false;
}
public sealed class Person
{
public string Name { get; set; } = string.Empty;
public int Age { get; set; }
}
}
- Launch the app
- Click a header to set a sort
- Click the clear sort button
Expected behavior Column sorts are cleared and the user cannot provide any futher sorts
Observed behavior Column sorts are not cleared and the user cannot provide any further sorts
Workaround Post a callback to the main thread dispatcher instead of setting CanUserSortColumns synchronously.
foreach (var column in DataGrid.Columns)
{
column.ClearSort();
}
Dispatcher.UIThread.Post(() => DataGrid.CanUserSortColumns = false);
Screenshots run the reproducible example
Desktop (please complete the following information):
- OS: Windows 11
- Version: 0.10.21
Additional context Add any other context about the problem here. DataGridTest.zip
Issue Analytics
- State:
- Created 3 months ago
- Comments:5 (4 by maintainers)
Top GitHub Comments
Here’s examples with
DataGridCollectionView
wrapper where this works as expected, both in code-behind and with a view model.SortClearTest.zip SortClearTest_ViewModel.zip
@VMelnalksnis best if you can file a minimal sample showing your issue.