Accidental De-Virtualization

WPF’s UI Virtualization allows you to bind a large collection of data to a control without requiring the control to create a UI Element for each item in your collection. It does this by using Virtualizing Panels such as the VirtualizingStackPanel.

Recently I had a collection of 15,000 elements being bound to a ListView ( a different issue) and it was taking up seconds to do so!! After a frustrating performance search looking for rogue server calls on the UI Thread I opened up Snoop to discover 15,000 ListViewItems in my VirtualizingStackPanel; WTF?

There are a number of things that can disable the Virtualization of a control (this is not an exhaustive list of reasons why virtualization may not be working)

  • Not using DataBinding
  • VirtualisationMode, IsVirtualising, CanContentScroll properties set incorrectly
  • Grouping Enabled
  • Changing the ControlTemplate or ItemsPanel to a non-virtualizing panel
  • Not supported by the control

In my case, as verified by Snoop, my ListView was using a VirtualizingStackPanel. I had however overridden the wrong control template in order to get fixed headers and had rendered my virtualization moot. (By having my ItemsPresenter not being directly descendant of my ScrollViewer)

If your having trouble binding large sets of data, and you need to do it, ensure your using a Virtualizing Panel (and that there’s only a handful of items in it).

Advertisements
Tagged , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: