1
votes

I'm developing a Silverlight application that displays items in a listbox control and I've run into a bit of a performance issue.

Each item in the listbox is a custom stackpanel with some formatted text and such.

When I've got a list of 500 or less items the listbox works fine, but loading more than this causes problems. At 1000 items, Silverlight will consume 10% cpu, even if I'm not doing anything, at 3000 items the cpu constantly uses 32-36%.

This is on a dual core machine, on an older machine I tested out on the cpu usage goes way up.

This also effects the framerate, I'm getting 6fps with a 3000 item listbox, which makes the application sluggish.

Does anyone know what might be causing this? My first thought was that silverlight is trying to render all the items, even though the items are off screen... this seems to be consistant as if I insert items with their Visability.Collapsed, the extra cpu overhead is not present.

PS: I'm running in windowless="true" as I need to display some html ontop of my silverlight form.

5

5 Answers

3
votes

You should use the DataGrid in Silverlight 2 because it supports UI Virtualization. It has been tested with millions of items and will only create enough visuals needed to display.

1
votes

Your guess is basically correct. Although Silverlight does not attempt to render all 3000 elements in the ListBox, it still needs to create 3000 ListBoxItem objects, which in turn get Measured and Arranged during layout time, etc, only for them to get clipped at render time. Layout happens much faster when the elements are Collapsed (since there is basically nothing for layout to do in this scenario).

WPF has VirtualizingStackPanel which would solve this problem, unfortunately Silverlight doesn't have this element.

1
votes

+1 for using the Silverlight DataGrid in this scenario, make sure you have the latest version installed as the default Silverlight SDK version has a few bugs.

Another option is to use the free Silverlight DataGrid Control available here. One of it's features is also a Virtual StackPanel Row Container which means the grid can handle an unlimited number of rows.

Compare the performance of each and see which works best in your situation.

0
votes

Just an observation - it looks like this is not an issue anymore with Silverlight 4. Adding 100K elements to a data-templated list box is instantaneous and it renders just fine as well.

0
votes

This helped me: Silverlight DeferredLoadListBox . It is written by David Anson, a Microsoft employee.

The DeferredLoadListBox derives from ListBox but has much better performance.