问题排序数据网格 [英] Issue sorting datagrid

查看:84
本文介绍了问题排序数据网格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很长时间以来一直在WPF中进行排序的一种技术是创建CollectionViewSource并指定SortDescriptions,例如

A technique I have used for a long time to do sorting in WPF is to create a CollectionViewSource and specifying SortDescriptions, eg

<Window.Resources>
    <CollectionViewSource x:Key="testView">
        <CollectionViewSource.SortDescriptions>
            <cm:SortDescription PropertyName="FirstName" Direction="Descending"></cm:SortDescription>
            <cm:SortDescription PropertyName="FamilyName"></cm:SortDescription>
        </CollectionViewSource.SortDescriptions>
    </CollectionViewSource>

这对我抛出的所有数据都非常适用,除了DataGrid。第一次看起来它运行良好,但是如果我更改数据,则网格将终止排序。对于使用相同CollectionViewSource的其他控件,它甚至会杀死它。我在这里创建了一个示例项目

This has worked perfectly for everything I've thrown at it except with the DataGrid. It appears to work fine the first time through but if I change the data then the grid kills the sorting. It even kills it for other controls that use the same CollectionViewSource. I've created a sample project here

http://www.mikesdriveway.com/misc/GridSortIssue.zip

要测试此问题,请运行项目,然后单击刷新数据按钮。请注意,项目顺序会更改。这只会发生一次,以再次测试停止并再次运行该项目。现在,从DataGrid中删除ItemsSource,然后再次运行项目。这次,当您单击刷新数据按钮时,没有任何反应,这意味着排序保持不变。网格以某种方式杀死了CollectionViewSource中的排序。这是一个错误吗?

To test this issue run the project and click the Refresh Data button. Notice that the order of items changes. This only happens once, to test again stop and run the project again. Now delete the ItemsSource from the DataGrid and run the project again. This time when you click the Refresh Data button nothing happens, this means the sorting remains the same. Somehow the grid is killing the sorting in the CollectionViewSource. Is this a bug?

干杯,
Michael

Cheers, Michael

推荐答案

这很奇怪,根据我的判断,底层 View SortDescription 会在源代码被清除时清除进行了更改,因此,如果不重新应用CVS的 SortDescriptions ,将不再进行排序。

That is rather odd, from what i can tell the SortDescription of the underlying View get cleared out when the source is changed, so if the SortDescriptions of the CVS are not re-applied there is no sorting anymore.

一个解决方法是重置 SortDescriptions

One workaround would be to "reset" the SortDescriptions:

var cvs = (CollectionViewSource)Resources["testView"];
var descriptions = cvs.SortDescriptions.ToArray();
cvs.SortDescriptions.Clear();
foreach (var descripton in descriptions) cvs.SortDescriptions.Add(descripton);

这篇关于问题排序数据网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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