6

Is there an implementation of PagedCollectionView in WPF around? It exists in Silverlight but isn't in WPF.

If there isn't, what would be the simplest way to implement this?

Phaded
  • 99
  • 1
  • 1
  • 6
  • Possible duplicate of http://stackoverflow.com/questions/784726/how-can-i-paginate-a-wpf-datagrid – WPF-it Nov 04 '11 at 05:50

2 Answers2

2

You can simply take the code from the Silverlight one and use that in your WPF project.

Kent Boogaart
  • 175,602
  • 35
  • 392
  • 393
  • Sorry I know this is really old but I feel I'm missing something here, there seems to be a whole bunch of dependencies which would also need porting? – Paulie Waulie Aug 20 '13 at 14:11
  • It's been a long time. I believe there are some dependencies, but they're all very localised and easy to copy across. – Kent Boogaart Aug 20 '13 at 22:46
  • 2
    Thanks Kent, I found this link which contained everything required : https://silverlight.svn.codeplex.com/svn/Release/Silverlight4/Source/System.Windows.Data/PagedCollectionView/ for anyone else finding this. – Paulie Waulie Aug 21 '13 at 08:30
  • Do you need to download the source code or can you import the apparently native `DataPager` control? https://learn.microsoft.com/en-us/previous-versions/windows/silverlight/dotnet-windows-silverlight/dd538632(v%3Dvs.95) – Jonathan Tuzman Jun 20 '20 at 15:06
1

Or use only the CollectionView class and "double filter" your collection

solution found here: Own CollectionView for paging, sorting and filtering

I've pasted the code snipet here for your convinience:

        // 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);
        };
Community
  • 1
  • 1
Sam L.
  • 207
  • 2
  • 3
  • @Edward For the same reason you bothered to comment on it? – Skrymsli Oct 28 '19 at 15:31
  • Using `IndexOf` on a collection view itself is complicated, but I& you can access that method, your filter predicate can just be `Math.Floor(collectionView.IndexOf(item) / iMaxItemPerPage) == iCurrentPage`. I haven’t tested this in my own implementation but I’m pretty sure it works! – Jonathan Tuzman Jun 20 '20 at 14:58
  • @JonathanTuzman, where must above solution actually be implemented? And how ? – Lucy82 May 07 '21 at 21:26
  • @Lucy82 I think it would be where the code above says “use your own filter” – Jonathan Tuzman May 08 '21 at 23:24