question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Datagrid ClearSort doesn't work if CanUserSortColumns subsequently synchronously set to false

See original GitHub issue

Describe 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; }
    }
}
  1. Launch the app
  2. Click a header to set a sort
  3. 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:open
  • Created 3 months ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
VMelnalksniscommented, Jul 4, 2023

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

0reactions
timuniecommented, Jul 4, 2023

@VMelnalksnis best if you can file a minimal sample showing your issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

wpf - CanUserSortColumns in datagrid doesn't make effect?
I have a datagrid with binding item source. I have set CanUserSortColumns property of datagrid into TRUE and so do with all inner...
Read more >
DataGrid.CanUserSortColumns Property (System.Windows ...
If the DataGridColumn.CanUserSort property and the DataGrid.CanUserSortColumns property are both set, a value of false takes precedence over a value of true ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found