Martin Macak
Martin Macak

Reputation: 3831

ViewModel and endless collection

I need to implement endless listbox (the one that is loading chunks of data when user scroll to the end) and I am using ViewModel.

I have ListBox bound to ObservableCollection, but I don't known how to get notified about reaching the end of it and yet not to break the MVVM paradigm. How can I achieve this?

The only thing I need to know is how to retain MVVM features and constraints and yet to get notified when the collection has been enumerated to it's end.

Thanks.

EDIT

I have been digging around a little and I am afraid that this is not an easy solution. I think the only possibility is to implement custom collection which will implement ICollection and INotifyCollectionChanged. But I don't know how the internal mechanisms of ListBox and other ItemsSource enabled controls work so I don't know how to implement lazy loading.

Is the primary fetching mechanism of WP controls which utilize ItemsSource the IEnumerable interface? Or does it use indexer methods? How does it call count? Does it depend on count or can it be changed during it's lifecicle (and in which calls? I think that not all calls can change the Count and then call NotifyCollectionChanged).

Is there any documentation? Or is there any implementation available?

Upvotes: 1

Views: 103

Answers (2)

Blachshma
Blachshma

Reputation: 17395

As this should support MVVM, I wouldn't tie the event to part of the listbox but as part of the Collection itself. You should attempt to add more chunks when the requested index is towards the end of the list.

One way to achive this is to inherit from IList and monitor the this[int index] property.

For instance:

public class OC<T> : IList<T>, IList
{
    public T this[int index]
    {
        get
        {
            if  (index >= (this.Count - 20)) // Insert correct heuristics here!
            {
               // Load more chunks
            }
            return base[index];
        }
    }
}

(Of course Count - 20 is not the best way to do this and you probably want to enter your own heuristics (maybe if the index is in the last 10%), but it shows the concept)

This way, you don't care if the View is using a ListBox or a combo or whatever - You're keeping the MVVM pattern. When the current item requested is an item towards the end of the list, that's your queue to start loading more chunks...

Note that you'll want to derive from IList too since for Virtualization, WPF seems to check for an IList (no generics) and IList<T> does not derive from IList. Long story short, if you don't derive from IList as well, the indexer will be called for each item in the list.

Upvotes: 2

Martin Macak
Martin Macak

Reputation: 3831

I found a nice solution that does not break MVVM and yet supplies quite elegant solution to this problem (it seems on the first look, I haven't investigated it properly yet.) Look at http://danielvaughan.orpius.com/post/Scroll-Based-Data-Loading-in-Windows-Phone-7.aspx

There's also stackoverflow topic for that, which I overlooked. Look at WP7 Auto Grow ListBox upon reaching the last item.

Upvotes: 1

Related Questions