我有一个列表框,里面有太多项目,并且 UI 变得越来越慢(虚拟化已打开,等等)。因此,我正在考虑仅显示前 20 个项目,并允许用户浏览结果集(即 ObservableCollection)。
有人知道 ObservableCollection 是否存在分页机制吗?以前有人这样做过吗?
Thanks!
此功能在 ObservableCollection 基类中不能直接使用。您可以扩展 ObservableCollection 并创建一个自定义集合来执行此操作。您需要将原始 Collection 隐藏在这个新类中,并根据 FromIndex 和 ToIndex 动态地将项目范围添加到该类中。重写InsertItem 和RemoveItem。我在下面给出了一个未经测试的版本。但请将此视为伪代码。
//This class represents a single Page collection, but have the entire items available in the originalCollection
public class PaginatedObservableCollection : ObservableCollection<object>
{
private ObservableCollection<object> originalCollection;
public int CurrentPage { get; set; }
public int CountPerPage { get; set; }
protected override void InsertItem(int index, object item)
{
//Check if the Index is with in the current Page then add to the collection as bellow. And add to the originalCollection also
base.InsertItem(index, item);
}
protected override void RemoveItem(int index)
{
//Check if the Index is with in the current Page range then remove from the collection as bellow. And remove from the originalCollection also
base.RemoveItem(index);
}
}
更新:我在这里有一篇关于这个主题的博客文章 -http://jobijoy.blogspot.com/2008/12/pagulated-observablecollection.html并将源代码上传到Codeplex.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)