如何对 ObservableCollection 进行分页?

发布于 2024-07-10 16:27:54 字数 193 浏览 6 评论 0原文

我有一个列表框,里面有太多项目,并且 UI 变得越来越慢(虚拟化已打开,等等)。 因此,我正在考虑仅显示前 20 个项目并允许用户浏览结果集(即 ObservableCollection)。

有人知道 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).

Does anybody know if a Pagination mechanism exist for the ObservableCollection? Does anybody has done that before?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

春风十里 2024-07-17 16:27:55

此功能在 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/pagination-observablecollection.html 并将源代码上传到 Codeplex

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);
    }
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文