【问题标题】:Paged Collection View in WPFWPF 中的分页集合视图
【发布时间】:2011-12-21 16:13:37
【问题描述】:

在 WPF 中是否有 PagedCollectionView 的实现?它存在于 Silverlight 中,但不在 WPF 中。

如果没有,最简单的实现方法是什么?

【问题讨论】:

标签: c# wpf paging collectionview


【解决方案1】:

您可以简单地从Silverlight one 中获取代码并在您的 WPF 项目中使用它。

【讨论】:

【解决方案2】:

或仅使用 CollectionView 类并“双重过滤”您的收藏

在此处找到解决方案:Own CollectionView for paging, sorting and filtering

为了您的方便,我已将代码片段粘贴在这里:

        // obtenir la CollectionView 
        ICollectionView cvCollectionView = CollectionViewSource.GetDefaultView(this.Suivis);
        if (cvCollectionView == null)
            return;

        // filtrer ... exemple pour tests DI-2015-05105-0
        cvCollectionView.Filter = p_oObject => { return true; /* use your own filter */ };

        // page configuration
        int iMaxItemPerPage = 2;
        int iCurrentPage = 0;
        int iStartIndex = iCurrentPage * iMaxItemPerPage;

        // déterminer les objects "de la page"
        int iCurrentIndex = 0;
        HashSet<object> hsObjectsInPage = new HashSet<object>();
        foreach (object oObject in cvCollectionView)
        {
            // break if MaxItemCount is reached
            if (hsObjectsInPage.Count > iMaxItemPerPage)
                break;

            // add if StartIndex is reached
            if (iCurrentIndex >= iStartIndex)
                hsObjectsInPage.Add(oObject);

            // increment
            iCurrentIndex++;
        }

        // refilter
        cvCollectionView.Filter = p_oObject =>
        {
            return hsObjectsInPage.Contains(p_oObject);
        };

【讨论】:

  • @Edward 出于同样的原因你懒得评论它?
  • 在集合视图上使用IndexOf 本身很复杂,但是我&你可以访问该方法,你的过滤谓词可以只是Math.Floor(collectionView.IndexOf(item) / iMaxItemPerPage) == iCurrentPage。我没有在自己的实现中测试过这个,但我很确定它可以工作!
  • @JonathanTuzman,上述解决方案必须在哪里实际实施?以及如何?
  • @Lucy82 我认为上面的代码会说“使用你自己的过滤器”
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-12
  • 2020-10-26
相关资源
最近更新 更多