How to break an ObservableCollection?

I have a ListBox with too many elements in it, and the user interface is getting slower and slower (virtualization enabled, etc.). So I thought about displaying only the first 20 elements and letting the user navigate through the result set (i.e., ObservableCollection).

Does anyone know if there is a Pagination mechanism for an ObservableCollection? Has anyone done this before?

Thanks!

+4
source share
1 answer

This object is not available directly in the base class ObservableCollecton. You can extend the ObservableCollection and create your own collection that does this. You need to hide the original collection in this new class and, based on FromIndex and ToIndex, dynamically add a range of elements to the class. Override InsertItem and RemoveItem. I give an unverified version below. But please take this as a 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 on this topic - http://jobijoy.blogspot.com/2008/12/paginated-observablecollection.html , and the source code is uploaded to Codeplex .

+4
source

All Articles