WPF DataGrid 列 header 排序箭头在更改其视图的来源后消失 collection 来源

WPF DataGrid column header sort arrow disappear after changing the source of its view collection source

我有以下简单的代码可以重现这个问题:

XAML:

<DataGrid ItemsSource="{Binding Source.View}"
          AutoGenerateColumns="False">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Name"
                            Binding="{Binding Name}"
                            SortMemberPath="Name"
                            SortDirection="Ascending"/>
    </DataGrid.Columns>
</DataGrid>

查看模型:

private readonly ObservableCollection<Data> _collection1 = new ObservableCollection<Data> {new Data("item 1"), new Data("item 2")};
private readonly ObservableCollection<Data> _collection2 = new ObservableCollection<Data> {new Data("person 1"), new Data("person 2")};

public MainViewModel()
{
    Source.Source = _collection1;

    // Set up a timer that runs 5 seconds.
    Observable.Timer(TimeSpan.FromSeconds(5)).ObserveOn(AsyncOperationManager.SynchronizationContext).Subscribe(_ =>
    {
        // Get existing sort descriptions.
        var existingSortDescription = Source.View.SortDescriptions.ToList();

        // Change source.
        Source.Source = _collection2;

        // This has to be done in order to maintain the sort order.
        existingSortDescription.ForEach(Source.SortDescriptions.Add);
    });
}

public CollectionViewSource Source { get; } = new CollectionViewSource();

private class Data
{
    public Data(string name)
    {
        Name = name;
    }

    public string Name { get; }
}

所以上面的代码所做的是,当应用程序启动时,使用 _collection1 作为数据网格的项目源的来源。

5 秒后,将数据网格的项目源更改为 _collection2

如果你运行上面的代码,"Name"列header中的排序方向箭头会在源更改为_collection2后立即消失,但是排序还是正确的。

这是 WPF DataGrid 控件中的错误还是我遗漏了什么?

您在视图模型中添加到 CollectionViewSourceViewSortDescriptions 不会影响您在 DataGrid 控件中看到的箭头视图。

您可以通过设置 SortDirection 属性 以编程方式显示特定列的箭头。所以你可以做的是创建一个自定义的 DataGrid 控件来为你处理这个(内置的不像你已经发现的那样),例如:

public class CustomDataGrid : DataGrid
{
    protected override void OnItemsSourceChanged(IEnumerable oldValue, IEnumerable newValue)
    {
        base.OnItemsSourceChanged(oldValue, newValue);

        INotifyCollectionChanged oldView = oldValue as INotifyCollectionChanged;
        if (oldView != null)
            oldView.CollectionChanged -= View_CollectionChanged;

        INotifyCollectionChanged newView = newValue as INotifyCollectionChanged;
        if (newView != null)
            newView.CollectionChanged += View_CollectionChanged;
    }

    private void View_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
        ICollectionView view = sender as ICollectionView;
        if (view != null)
        {
            SortDescription sd = view.SortDescriptions.LastOrDefault();
            if (sd != null)
            {
                DataGridColumn column = Columns.FirstOrDefault(x => x.SortMemberPath == sd.PropertyName);
                if (column != null)
                {
                    column.SortDirection = sd.Direction;
                }
            }
        }
    }
}

然后您只需将 <DataGrid /> 元素替换为 XAML 中的 <local:CustomDataGrid /> 元素即可。