如何对ObservableCollection进行分页? [英] How to paginate an ObservableCollection?

查看:215
本文介绍了如何对ObservableCollection进行分页?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表框,其中的项目太多,UI越来越慢(虚拟化处于启用状态,等等).因此,我考虑只显示前20个项目,并允许用户浏览结果集(即ObservableCollection).

I have a ListBox with way too many items in it and the UI is getting slower and slower (virtualization is on, etc). So I was thinking about displaying only the first 20 items and allow to user to navigate through the result set (i.e. ObservableCollection).

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

谢谢!

推荐答案

此功能在ObservableCollecton基类中不直接可用.您可以扩展ObservableCollection并创建执行此操作的自定义Collection.您需要在这个新类中隐藏原始Collection,并基于FromIndex和ToIndex给出将项目范围动态添加到该类中的信息.覆盖InsertItem和RemoveItem.我给一个未经测试的版本下面.但是请把它当作伪代码.

This facility is not directly available in the base ObservableCollecton class. You can extend the ObservableCollection and create a custom Collection which does this. You need to hide the original Collection inside this new class and based on a FromIndex and ToIndex give dynamically add the range of items to the class. Override InsertItem and RemoveItem. I am giving a not-tested version bellow. But please take this as just pseudo code.

 //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: //jobjoboy.blogspot.com/2008/12/paginated-observablecollection.html ,并将源代码上传到

UPDATE: I have a blog post about this topic on here - http://jobijoy.blogspot.com/2008/12/paginated-observablecollection.html and the source code is uploaded to Codeplex.

这篇关于如何对ObservableCollection进行分页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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