4

我有一个 ListBox,里面有太多的项目,而且 UI 越来越慢(虚拟化等)。所以我在考虑只显示前 20 个项目并允许用户浏览结果集(即 ObservableCollection)。

有人知道 ObservableCollection 是否存在分页机制吗?以前有人做过吗?

谢谢!

4

1 回答 1

4

此工具在基本 ObservableColleton 类中不直接可用。您可以扩展 ObservableCollection 并创建执行此操作的自定义 Collection。您需要将原始 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/paginated-observablecollection.html并且源代码已上传到Codeplex

于 2008-12-01T04:18:40.250 回答