Alex Marshall
Alex Marshall

Reputation: 10312

How does VirtualizingStackPanel decide when to unload (dispose?) of virtualized controls?

I'm working with an ItemsControl using a VirtualizingStackPanel in its ControlTemplate. I've got the virtualization working, to some extent. I've put debugging statements in the Loaded and Unloaded event handlers for the controls in my DataTemplate for the items, but they don't get unloaded immediately after the controls go out of view. Is there any way to force this behaviour ?

Upvotes: 3

Views: 1224

Answers (1)

zpinter
zpinter

Reputation: 2242

You might have some luck setting the VirtualizationMode to Recycled. There are comments in the source code for VirtualizingStackPanel.cs that indicate Recycled mode immediately cleans up renderers (instead of doing it in the background):

    // 
    // Delayed Cleanup is used when the VirtualizationMode is standard (not recycling) and the panel is scrolling and item-based
    // It chooses to defer virtualizing items until there are enough available.  It then cleans them using a background priority dispatcher 
    // work item
    //

Note, you can find the full source for VirtualizingStackPanel here:

http://referencesource.microsoft.com/netframework.aspx

Upvotes: 2

Related Questions