I was working on one of my projects today and I noticed that one of our popups displaying search results in a ListBox was having really serious performance problems. After determining that the problem was, in fact, on the WPF side of things, I was somewhat baffled. I wasn’t doing anything that I could think of that should be pushing the limit of what WPF could do.
Finally, I went looking online for an answer and discovered a list of possible performance killers for the ListView (and ListBox) on Mark Shurmer’s blog. Chief among his no-no’s:
Embedding the ListView inside a StackPanel
Which is exactly what I was doing.
Why is this a problem? To answer that question, let’s take a look at the ItemsPanel at runtime using Snoop. When I place my ListBox into a Grid, here is what my ItemsPanel looks like:
If I change back to what I originally had (my ListBox in a StackPanel), here is what my ItemsPanel looks like:
That’s actually not even the end of it. That’s about half.
As far as I can tell, all of Mark’s suggestions have to do with making sure that the VirtualizingStackPanel can do what it needs to do in order to keep your program running at a reasonable rate. Good stuff.