WPF DataGrid忽略了SortDescription [英] WPF DataGrid ignores SortDescription

查看:231
本文介绍了WPF DataGrid忽略了SortDescription的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里有一个奇怪的问题是关于WPF DataGrid(.NET 4.0中System.Windows.Controls.DataGrid)的排序。



它的ItemsSource绑定到数据文本对象的属性:

 < DataGrid HeadersVisibility =ColumnSelectedIndex =0MinHeight =30ItemsSource ={Binding FahrtenView}AutoGenerateColumns =Falsex:Name =fahrtenDG> 

FahrtenView如下所示:

  public ICollectionView FahrtenView 
{
get
{
var view = CollectionViewSource.GetDefaultView(_fahrten);
view.SortDescription.Add(new SortDescription(Index,ListSortDirection.Ascending));
返回视图;
}
}

DataGrid被排序。但是它第一次被分配了一个DataContext就被分类了。之后,更改DataContext(通过在数据层次结构中选择另一个parental对象)仍然会导致对属性FahrtenView进行评估(我可以在其中放入BP并调试器停止),但添加的排序描述被完全忽略,因此排序不工作了在每个DataContextChange上甚至调用fahrtenDG.Items.Refresh()都不会有帮助,甚至是



我很确定这是WPF DataGrid排序方式,不是吗?那么为什么在第一次被打电话之后呢完全完成了工作后呢,就拒绝这样做呢?



任何想法?我将非常感激。



干杯,
Hendrik

解决方案

我已经从DataGrid继承下来,简要地了解其内容。我发现,由于某些神秘的原因,虽然第一次调用OnItemsSourceChanged 被调用,但是在OnItemsSourceChanged ItemSource的SortDescription列表的每个下一次调用中,一切都看起来不错集合视图为空



为此,我添加了一个在OnItemsSourceChanged末尾调用的自定义的安装过程描述事件。现在我在事件处理函数中添加了排序说明,它的工作就像一个魅力。



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



这是我重写的OnItemsSourceChanged

  protected override void OnItemsSourceChanged(IEnumerable oldValue ,IEnumerable newValue)
{
if(SetupSortDescriptions!= null&&(newValue!= null))
SetupSortDescriptions(这是新的ValueEventArgs< CollectionView>((CollectionView)newValue)) ;

base.OnItemsSourceChanged(oldValue,newValue);
}


I've got a strange problem here regarding sorting of a WPF DataGrid (System.Windows.Controls.DataGrid in .NET 4.0).

Its ItemsSource is bound to a property of the datacontext object:

<DataGrid HeadersVisibility="Column" SelectedIndex="0" MinHeight="30" ItemsSource="{Binding FahrtenView}" AutoGenerateColumns="False" x:Name="fahrtenDG">

FahrtenView looks like this:

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

The DataGrid gets sorted. However it only gets sorted the very first time it's assigned a DataContext. After that, changing the DataContext (by selecting another "parental" object in a data hierarchy) still causes the property FahrtenView to be evaluated (I can put a BP in and debugger stops there) but the added sortdescription is completely ignored, hence sorting does not work anymore.

Even calling fahrtenDG.Items.Refresh() on every DataContextChange doesn't help.

I'm pretty sure this is the way to go when it comes to sorting a WPF DataGrid, isn't it? So why does it refuse to work so obstinately after doing its job perfectly the very first time it gets called?

Any idea? I'd be very grateful.

Cheers, Hendrik

解决方案

I've inherited from DataGrid to catch a brief glimpse on its guts. What I've found is that for some mysterious reasons, although the first time OnItemsSourceChanged gets called, everything looks fine, in every following call of OnItemsSourceChanged the SortDescription list of the ItemsSource collection view is empty.

For that reason I've added a custom SetupSortDescription event that is called at the end of OnItemsSourceChanged. Now I'm adding the sort descriptions in the event handler function, which's working like a charm.

I consider this a bug in the WPF toolkit DataGrid.

Here's my overridden 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);
    }

这篇关于WPF DataGrid忽略了SortDescription的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆