Own CollectionView for paging, sorting and filtering

故事扮演 提交于 2019-12-01 11:30:18
Sivakumar

As mentioned in this, you can take code from Silverlight and use that in WPF.

Paged Collection View in WPF

Here is a solution for filtering and paging using only the CollectionView class.

You just need to set the current page index and max items per page to fits your needs.

        // 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 cvCollectionView.Contains(p_oObject) && hsObjectsInPage.Contains(p_oObject);
        };
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!