Showing data collection in WPF is quite easy. All you need to do is to prepare your data collection and assign it to ItemsControl.ItemsSource.
But, when we target on a lager set of data (ex: 100k items with complex data template.), here comes the performance issues if you don’t handle it carefully.
First, it might be time-consuming to create 100k items in one time. So you will suffer the first UI sluggish here. (Of course you can use a worker thread to create the data to avoid this sluggish.)
Then you assign the collection to ItemsControl. The WPF layout system will create UI objects for items. Here the second UI sluggish occurs.
If your app doesn’t consume too many memory, you are good from here. But, generally, the overall memory consumption is pretty high now and make the whole application become less responsive.
The goal of virtualization is to minimize the resource usage (ex:computation power / memory ). Consume resource when only necessary.
Virtualization is also a key to scaliblity. Think about the 100k items. No matter how fast an operation is, it won’t fast enough when it times 100k.
We do need a way to get rid of influence of numbers.
Roughly, according to the place of virutalizing skills apply, we can divide them into two categories, UI virtualization and Data virtualization
In .Net 3.5, WPF provides UI virtualization to ease the pain of handling large data set.
- Virtualizaing Panel
You can make your itemsControl’s panel as a virtualizing panel. By doing so, the layout system will create only the visible items during the layout pass.
Then you can tell the ItemContainerGenerator to reuse the created UI objects by enable the recycling mode to save the time of expanding DataTemplate.
Here is a good start serial. Follow this series, Implementing a virtualized panel in WPF, you are able to create your own virtualizing panel.
(Note: There is an enhance chance at MeasureOverride() function. Please pay more attention to that function.)
Having a built-in UI virtualization support is a pretty good thing. It does improve the performance alot. But only in UI level, it is not good enough.
If the bottleneck is at data level,the UI virtualization can’t help here. Then we are still suffering the bad performance.
In data level, we don’t have Microsoft’s support. We are on our own now.
There are already four great posts about WPF data virtualization.
- WPF: Data Virtualization by Paul McClean
- Data Virtualization in WPF and beyond by Vicent
- Data virtualization by Bea Stollnitz
- How can I improve on existing WPF data virtualization solutions? by Bea Stollnitz
After read through those posts, you can get a better understanding about what data virtualization is, what problems might happen and how it fits to WPF.
Those solutions have the same limitation. That is the data collection is read-only. CollectionChanged event is not well-handled. Please Bea Stollnitz’s post for more details.
So far as I study, to introduce data virtualization, you have to deal with these issues.
- Virtualize your data / cache mechanism
- Selection (Select limited items / select all)
- implement your own VirtualizingPanel
- CollectionChanged notification.
- Grouping / Sorting / Filtering
In implement part, you might need to create your own classes for more detailed controls.
I’m going to implement my own version of WPF data virtualization.
Hopefully I extend the existing solution.