WPF VirtualizingStackPanel for increased performance

bluebit picture bluebit · Sep 7, 2009 · Viewed 34.1k times · Source

I would like a simple description of how to implement a virtualizingstackpanel for an ItemsControl that is databound to an ObservableCollection in my MVVM.

I have an ItemsControl instance for each tab in a tab control, and switching tabs becomes VERY slow when the ItemsControl grows larger.

What can I do to speed up the app?

I opened up a WPF profiler and saw that each element (which is a custom user control) displayed in my ItemsControl of each tab had its own ContentPresenter. So I essentially had 100 content presenters all running for 100 items in my ObservableCollection in MVVM. Is this corrrect? How can I optimize?

Answer

Mike Two picture Mike Two · Sep 21, 2009

There are two techniques that might be a big help. Both of them are described very well by Bea Stolnitz on her blog.

The first is UI Virtualization and the second is Data Virtualization

In UI virtualization you use things like VirtualizingStackPanel to make the UI draw fewer things.

Data virtualization makes sure you don't bring a million objects into memory when you are only going to show 100.

So UI virtualization minimizes the number of things drawn and data virtualization minimizes the number of things that could be drawn.

Hope that helps