当按特定列排序时,WPF DataGrid会丢失虚拟化 [英] WPF DataGrid loses virtualization when sorted by specific columns

查看:208
本文介绍了当按特定列排序时,WPF DataGrid会丢失虚拟化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用绑定到实现IList(而不是IEnumerable)的自定义ItemsSource的WPF DataGrid。自定义ItemsSource执行数据虚拟化,并根据需要加载项目页面。 MyDataGrid实际上从DataGrid继承并覆盖了DataGrid排序方法的处理,以便我可以在排序时维护数据虚拟化。我的MyDataGrid打开了UI虚拟化。



当我运行应用程序时,MyDataGrid显示的很好,告诉我我有大约20,000个项目,只询问我的ItemsSource前20个左右的物品。然后我可以点击列标题来排序各个列,而且我的ItemsSource只需要重新加载40个项目的第一页,因为MyDataGrid要求仅刷新前20个。



但是,当我点击我的姓氏列进行排序时,MyDataGrid会丢失其UI虚拟化,并要求我的ItemsSource加载每个单个项目,即使它只需要前20个等等。我可以通过索引来查看每一个项目(这个[0])。



我试过研究ICollectionView,UI虚拟化和数据虚拟化,唯一的我看过的这个似乎可能适用的事情是关于多行选择(在我已经将MyDataGrid的SelectionMode设置为单,所以这不应用。



另一个提示可能是,在按姓氏排序之前,MyDataGrid中的第一个项目的名字以字母V(字母表中的较晚)开始,并且实际上可以按姓氏按顺序对列表进行排序,而不会丢失虚拟化。同样的事情发生在我的标题列,其第一个项目以字母S开头。我没有任何其他列的问题。



任何想法?

解决方案

通过调用堆栈看,VirtualizingStackPanel的MeasureOverride中出现问题,我注意到它正在调用SyncUniformSizeFlags方法。所以我进入DataGrid的默认样式,并将RowHeight设置为固定的数量,并且不再会导致问题。



所以,看来DataGrid可以当固定的RowHeight未设置时,失去其虚拟化。仍然不知道为什么选择按姓氏或标题按升序排序,导致它想重新同步行高,但我可以解决这个问题。


I am using a WPF DataGrid that is bound to a custom ItemsSource that implements IList (and NOT IEnumerable). The custom ItemsSource performs data virtualization and only loads pages of items as needed. MyDataGrid actually inherits from DataGrid and overrides the handling of the DataGrid sort methods so that I can maintain data virtualization while sorting. I have UI Virtualization turned on for MyDataGrid.

When I run the application, MyDataGrid displays just fine, tells me I have roughly 20,000 items, and only asks my ItemsSource for the first 20 or so items. I then can click on column headers to sort various columns and again my ItemsSource only has to reload the first page of 40 items because MyDataGrid has asked to refresh only the first 20 or so.

BUT, when I click on my Last Name column to sort, MyDataGrid loses its UI Virtualization and asks my ItemsSource to load every single item even though it only needs the first 20 or so. I can watch as it asks for every single item by index (this[0]).

I've tried researching ICollectionView, UI Virtualization, and Data Virtualization and the only thing that I've read that seems it may apply is regarding multiple row selection (at DataGrid row request patterns with data virtualization I've set MyDataGrid's SelectionMode to Single so this shouldn't apply.

Another hint may be that, prior to sorting by Last Name, the first item in MyDataGrid has a last name that starts with the letter V (very late in the alphabet), and that I can actually sort the list in Descending order by last name without losing virtualization. The same thing happens with my Title column which has a first item that starts with the letter S. I don't have the problem with any of the other columns.

Any ideas?

解决方案

FIGURED IT OUT!!! Looking through the call stack, it appeared that the problem was happening during the MeasureOverride of the VirtualizingStackPanel and I noticed it was calling the SyncUniformSizeFlags method. So I then went into the default style for the DataGrid and set the RowHeight to a fixed amount and it no longer causes the problem.

So, it appears that the DataGrid can lose its virtualization when a fixed RowHeight is not set. Still don't know why opting to sort in ascending order by Last Name or Title caused it to want to resync the row heights, but I can work around that one.

这篇关于当按特定列排序时,WPF DataGrid会丢失虚拟化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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