WPF and WinRT (C# + XAML) both support UI virtualization using panels that support it such as VirtualizingStackPanel
and others. When using MVVM It's done using an ItemsControl
of some sort (ListBox
, GridView
, etc...) that is bound to an enumerable property on the view model (usually ObservableCollection). The items control creates the UI only for items that become visible. It's called UI virtualization because only the UI is virtualized. Only the view of items that are not presented is not created, and is deferred to the moment in time that the user actually scrolls to the item. The view models objects in the list are all created in advance. So if I have a list of 100,000 people to present, the ObservableCollection
will have to include 100,000 view models that are made regardless of when the user scrolls them into view.
In our application, we would like to implement it so that the view model layer is a part of this virtualization. We want the items control to present a scroll bar that fits the total number of items that can potentially be loaded (so the observable collection should make the items control think that it already contains 100,000 items, so that the scroll bar view port is in the right size), but we want the observable collection to be notified any time a new item is about to come into view so it can load the actual object from the server. We want to be able to show some sort of progress indicator inside loaded items and then replace it with the actual data template for the item as soon as it is loaded into the observable collection.
As much as possible, we would like to maintain the MVVM guidelines, but performance and responsiveness are a priority. We also prefer a reusable solution if at all possible.
What would be the best way to tackle this?