c# – 如何提高无虚拟化DataGrid上的排序性能?
我相信大多数人现在都会惊讶为什么我们必须关闭wpf datagrid的虚拟化.尽管它的真正虚拟化有助于减少内存占用,但它增加了CPU开销,滚动体验并不完美.
根据客户的要求,我们不得不在数据网格中禁用虚拟化,并进一步优化,现在它可以非常顺利地上下滚动,没有任何滞后.缺点是数据被预加载并保存在内存中.这是我们可以生活的解决方案. 然而,排序现在已经成为一个大问题.虽然使用CustomSorter是真的:IComparer将是对常见SortDecriptors的更好的排序替代方法,但是在我们的案例中几乎没有任何区别,因为整个行都被重绘. 有没有办法提高非虚拟化数据网格的排序速度? 非常感谢, 更新: 我遇到了一个想要实现的想法.取消绑定Itemssource,执行排序,一旦排序完成,重新绑定Itemssource. 为了实现这一点,我从DataGrid派生来捕获SortHandler(即当用户点击该列时) public class CustomSortDataGrid : DataGrid { public CustomSortDataGrid() { Sorting += SortHandler; } private void SortHandler(object sender,DataGridSortingEventArgs e) { DataGridColumn column = e.Column; IComparer comparer = null; // prevent the built-in sort from sorting e.Handled = true; ListSortDirection direction = (column.SortDirection != ListSortDirection.Ascending) ? ListSortDirection.Ascending : ListSortDirection.Descending; //set the sort order on the column column.SortDirection = direction; //use a ListCollectionView to do the sort. var lcv = (ListCollectionView)CollectionViewSource.GetDefaultView(ItemsSource); comparer = new BidYieldComparer(direction); //apply the sort lcv.CustomSort = comparer; } } 这将利用比SortDescriptors更好的Comparer Sorting. BindingOperations.ClearBinding(this,ItemsSourceProperty); 以上这一行将清除绑定. //apply the sort lcv.CustomSort = comparer; 理论上(不确定这是否是正确的方法)ItemsSource = lcv;会重新绑定但是表现还是一样的. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |