WPF DataGrid忽略SortDescription

Hen*_*ese 9 sorting wpf datagrid collectionview

关于WPF DataGrid(.NET 4.0中的System.Windows.Controls.DataGrid)的排序,我遇到了一个奇怪的问题.

其ItemsSource绑定到datacontext对象的属性:

<DataGrid HeadersVisibility="Column" SelectedIndex="0" MinHeight="30" ItemsSource="{Binding FahrtenView}" AutoGenerateColumns="False" x:Name="fahrtenDG">
Run Code Online (Sandbox Code Playgroud)

FahrtenView看起来像这样:

    public ICollectionView FahrtenView
    {
        get
        {
            var view = CollectionViewSource.GetDefaultView(_fahrten);
            view.SortDescriptions.Add(new SortDescription("Index", ListSortDirection.Ascending));
            return view;
        }
    }
Run Code Online (Sandbox Code Playgroud)

DataGrid已排序.但是它只在第一次分配DataContext时才进行排序.之后,更改DataContext(通过在数据层次结构中选择另一个"parental"对象)仍会导致对FahrtenView属性进行评估(我可以放入BP并调试器停在那里)但是添加的sortdescription完全被忽略,因此排序会不再工作了.

甚至在每个DataContextChange上调用fahrtenDG.Items.Refresh()也无济于事.

我很确定这是排序WPF DataGrid的方法,不是吗?那么,为什么它在第一次被调用后完美地完成工作后拒绝如此顽固地工作?

任何的想法?我会非常感激的.

干杯,亨德里克

Hen*_*ese 8

我继承了DataGrid,以便对其内容进行简要介绍.我发现的是,对于某些神秘的原因,虽然第一次OnItemsSourceChanged被调用,一切都看起来不错,在每一个下面的调用OnItemsSourceChanged的的ItemsSource集合视图的SortDescription列表是空的.

出于这个原因,我添加了一个在OnItemsSourceChanged结束时调用的自定义SetupSortDescription事件.现在我在事件处理函数中添加排序描述,它就像一个魅力.

我认为这是WPF工具包DataGrid中的一个错误.

这是我重写的OnItemsSourceChanged

    protected override void OnItemsSourceChanged(IEnumerable oldValue, IEnumerable newValue)
    {
        if (SetupSortDescriptions != null && (newValue != null)) 
            SetupSortDescriptions(this, new ValueEventArgs<CollectionView>((CollectionView)newValue)); 

        base.OnItemsSourceChanged(oldValue, newValue);
    }
Run Code Online (Sandbox Code Playgroud)


Jon*_*ker 6

我对Hendrik的回答有所改进,使用MVVM而不是事件.

    public static readonly DependencyProperty SortDescriptionsProperty = DependencyProperty.Register("SortDescriptions", typeof(List<SortDescription>), typeof(ReSolverDataGrid), new PropertyMetadata(null));

    /// <summary>
    /// Sort descriptions for when grouped LCV is being used. Due to bu*g in WCF this must be set otherwise sort is ignored.
    /// </summary>
    /// <remarks>
    /// IN YOUR XAML, THE ORDER OF BINDINGS IS IMPORTANT! MAKE SURE SortDescriptions IS SET BEFORE ITEMSSOURCE!!! 
    /// </remarks>
    public List<SortDescription> SortDescriptions
    {
        get { return (List<SortDescription>)GetValue(SortDescriptionsProperty); }
        set { SetValue(SortDescriptionsProperty, value); }
    }

    protected override void OnItemsSourceChanged(System.Collections.IEnumerable oldValue, System.Collections.IEnumerable newValue)
    {
        //Only do this if the newValue is a listcollectionview - in which case we need to have it re-populated with sort descriptions due to DG bug
        if (SortDescriptions != null && ((newValue as ListCollectionView) != null))
        {
            var listCollectionView = (ListCollectionView)newValue;
            listCollectionView.SortDescriptions.AddRange(SortDescriptions);
        }

        base.OnItemsSourceChanged(oldValue, newValue);
    }
Run Code Online (Sandbox Code Playgroud)