Recycling that Item Container
Item container recycling is a one of the many new features in SP1. I'm going to talk a little bit about it here.
What is it?
As a little background, the VirtualizingStackPanel’s virtualization scheme basically works like this: generate containers when needed and throw them away when they are no longer in view. This does solve the memory issue that is created when the panel has a large number of items, but it can still be very expensive to have to recreate and throw away containers every time they go out of view. Enter container recycling. Container recycling is a performance optimization to this virtualization scheme. It basically reuses existing containers so they do not have to be recreated each time they come back into view.
How do I use it?
VirtualizingStackPanel’s virtualization will still work the same as before. In fact container recycling will be turned off by default. I will explain how to turn it on shortly, but before that let me describe the new API. VSP (VirtualizingStackPanel) has added a new attached property, VirtualizationModeProperty, as well as the getter and setter for it, GetVirtualizationMode and SetVirtualizationMode. This property is used to describe the type of mode that it can be in, VirtualizationMode.Standard or VirtualizationMode.Recycling. The former will use the standard virtualization scheme just as it normally would and the latter will include recycling to the virtualization scheme. The VirtualizationModeProperty is to be set from the ItemsControl that hosts the items presented by the VSP. By default, it is set to VirtualizationMode.Standard.
Here is an example of turning on recycling on a ListBox:
<ListBox VirtualizingStackPanel.VirtualizationMode="Recycling" …/>
Note that ListBox uses a VSP as its ItemsPanel and sets the VirtualizingStackPanel.IsVirtualizing attached property to true by default. If you were using ItemsControl directly or creating a custom ItemsControl that uses a VSP as its ItemsPanel, you will need to set both attached properties to get the recycling behavior.
<ItemsControl VirtualizingStackPanel.IsVirtualizing="true" VirtualizingStackPanel.VirtualizationMode="Recycling" …/>
Just as with the virtualization scheme, marking containers as non-virtualizable will not virtualize or recycle the container. You can do this by handling VSP’s CleanUpVirtualizedItem event. This event is fired when a container is about to be thrown away or recycled. You have the option here of cancelling the operation.
Some other helpful methods for you to have more control of the data items and containers include ItemsControl.PrepareContainerForItemOverride which is called before a container is used and ItemsControl.ClearContainerForItemOverride which is called when a container is thrown away or recycled.
For more information regarding scrolling performance take a look at this article by one our dev's here on the WPF team. It came out before this feature so the information regarding container recycling is a suggestion on how to implement it and not how to actually use it. It still has a lot of great information though.