In the era of Win32/MFC the virtual listbox (and virtual listview) was a popular method to efficiently display large collections. In WPF there is no standard way to efficiently display a large collection of items. For each control that is derived from ItemsControl (such as the ListBox) there are two important items in the control:
- The UI element that is used to display the item.
- The actual .NET object that is represented by the UI element.
Most ItemsControl derived classes use a VirtualizingStackPanel object to display the items in the control. This object only creates the UI elements that are actually displayed on screen. If you have a Listbox containing 10.000 elements and only some are displayed on screen, then VirtualizingStackPanel only creates the UI elements for the visible items. This sounds great, but the underlying collection still contains 10.000 elements. If you need to collect these elements from another tier (i.e. via a webservice), then it takes a very long time to populate the listbox.
In the next paragraphs I will describe the concept of “virtual collections” to efficiently populate the collection. A virtual collection is a collection containing only stubs to the actual items. If an item is accessed, then it is created on demand. If only the first 10 elements in a 10.000 item collection are accessed, then only the first 10 elements are actually created. This can greatly improve performance for large collections that are expensive to populate.
ItemsControl derived class uses an ItemsSource property that accepts an object that implements the IEnumerable interface. It is much more efficient to supply an object that also implements the IList interface, because it supports indexed access to the underlying collection. I haven’t found any documentation, but I found out that if your collection support IList, then the IList interface is used in favor of the (less efficient) IEnumerable interface. The virtual collection that is presented here supports the IList interface (and implicitly support IEnumerable as well). That’s why it is compatible with all ItemsSource derived controls.
The concept is easy. During the creation of the virtual collection an array is allocated that only contains null pointers. When the indexing operator [] is used, then the underlying object is created and stored inside the array. The next time the same item is accessed, then it is already there and doesn’t need to be created anymore.
The virtual collection uses a generator to create the underlying objects. The object generator should implement the IObjectGenerator interface and must be provided by the programmer. This interface only has one property and one method:
- The Count property returns the number of items in the collection.
- The CreateObject method creates a new object based on the given index.
It is very easy to use the virtual collection. Implement an object that implements IObjectGenerator<T> and pass it to the VirtualList<T>‘s constructor. Refer to the attached example code for an example how to use and bind the collection.
In a next part I will show you how to add paging to IObjectGenerator to reduce the number of roundtrips. This is useful for collections that are large and the roundtrip to create the objects is expensive.


