Pregunta

Tengo un ListBox con demasiados elementos y la interfaz de usuario es cada vez más lenta (la virtualización está activada, etc.). Así que estaba pensando en mostrar solo los primeros 20 elementos y permitir al usuario navegar por el conjunto de resultados (es decir, ObservableCollection).

¿Alguien sabe si existe un mecanismo de Paginación para la Colección Observable? ¿Alguien ha hecho eso antes?

¡Gracias!

¿Fue útil?

Solución

Esta instalación no está directamente disponible en la clase base ObservableCollecton. Puede ampliar la colección ObservableCollection y crear una colección personalizada que haga esto. Debe ocultar la Colección original dentro de esta nueva clase y, basándose en FromIndex y ToIndex, agregar dinámicamente el rango de elementos a la clase. Reemplazar InsertItem y RemoveItem. Estoy dando una versión no probada a continuación. Pero tome esto como un pseudocódigo.

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

ACTUALIZACIÓN: Tengo una publicación de blog sobre este tema aquí - http: //jobijoy.blogspot.com/2008/12/paginated-observablecollection.html y el código fuente se carga en Codeplex .

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top