views:

478

answers:

2

Hi..

Iam using Silverlight 2 in which I have a perfomance issue when I use a Listbox control. I am binding a List of objects of about 500 records which is taking 3-4 seconds for databinding.

But Iam not having this issue with DataGrid.

Does anyone have a answer for improving the perfomance issue in Listbox?

+2  A: 

Without knowing additional context about your application, there could be a couple of things going on. The first that comes to mind is that you have a very "heavy" visual tree. Each item in your ListBox is going to create some graph of visual objects. The more you have, the longer it takes to render.

When those items are not visible on the screen, Silverlight is still doing the work to render them. Basically this means that the Layout() and Measure() pass are being performed on all of your data-bound ListBox items, even if they aren't visible.

The trick is to use virtualization to force Silverlight into not performing the Layout/Measure passes on controls that are not visible within the control's visible region at the time.

Here is a blog post that describes how to do this with a StackPanel. It shouldn't take much to abstract this to a ListBox or better yet, you could simply make the control container for your ListBox a virtualizing stack panel and then you'd be done :)

http://blogs.msdn.com/uberdemo/archive/2009/02/18/a-virtualizedstackpanel-control-in-silverlight-2-0.aspx

Kevin Hoffman
+4  A: 

The reason behind this is that the DataGrid supports UI Virtualization and the ListBox does not. This means that the DataGrid only creates visual elements for the visible items while the ListBox creates visual elements for all of its items. Whenever you have to select from a list of items that can be very large, use the DataGrid instead of the ListBox. You can style the DataGrid and use the TemplateColumn to make it look just like a ListBox.

Michael

Michael S. Scherotter