C# winforms上的虚拟列表视图排序
大家好, 我有一个winforms虚拟化的列表视图,我希望能够对其进行排序。 我实现了一个sorter类,该类派生自IComparer。 现在,每当我想对listview排序时,我都会执行以下操作:C# winforms上的虚拟列表视图排序,c#,winforms,listview,C#,Winforms,Listview,大家好, 我有一个winforms虚拟化的列表视图,我希望能够对其进行排序。 我实现了一个sorter类,该类派生自IComparer。 现在,每当我想对listview排序时,我都会执行以下操作: public void SortMyVirtualListView() { this.VirtualListSize = 0; // set virtual size to 0 _myInnerList.Sort(_myComparer); // sort the inner li
public void SortMyVirtualListView()
{
this.VirtualListSize = 0; // set virtual size to 0
_myInnerList.Sort(_myComparer); // sort the inner listlistview items source)
this.VirtualListSize = _myInnerList.Count; // re-set the virtual size to items count
}
我想确定的是-这真的是排序后重置虚拟listview项的方式吗?我真的必须将虚拟列表大小设置为0,然后将其重置为新大小吗
似乎我不能忽略关于将VirtualListSize设置为零的部分,然后在对基础列表重新排序后,将VirtualListSize设置回其原始值(this.VirtualListSize=\u myInnerList.Count)
对于winforms的优化(和可排序的)虚拟列表视图,我找不到任何令人满意的代码。设置VirtualListSize是否存在性能问题?这是有道理的,因为ListView不知道您的内部列表已经更改,所以您需要以某种方式告诉它。 如果以这种方式重置VirtualListSize会导致性能问题,那么仅仅重新绘制可见项可能会有所不同
_myInnerList.Sort(_myComparer); // sort the inner listlistview items source)
int startIndex = this.TopItem == null ? 0 : this.TopItem.Index;
int endIndex = Math.Min(startIndex + 100, this.VirtualListSize - 1);
this.RedrawItems(startIndex, endIndex, true);
(是的,我知道,我也不喜欢神奇的数字100…:)
编辑:在排序后调用Refresh()也应该可以。有人发布了一个答案,其中包括在内部列表的排序操作后调用Invalidate()。它起作用了。。但我想调用RedrawItems()会快得多,对吗?