Jonathan D
Jonathan D

Reputation: 1364

Silverlight ListBoxItem Poor Performance

I have list of object which i loop around and create controls from. i then put each of these controls into the content of a listboxitem and add it to a list box.

I seem to be getting very poor performance doing with. ive commented bits of my code out and it seems that if the content is filled with a control performance goes out the window. Creating 80 of these list boxes seems to take 4 minutes Any one know why or how i can solve this?

code sample:

foreach (var service in e.Result)
            {
                ListBoxItem lbi = new ListBoxItem();
                lbi.Tag = service.ServiceId;

                SmallServicePanel ssp = new SmallServicePanel();
                ssp.DisplayText = service.DisplayText;
                ssp.ServiceTemplateId = service.ServiceTypeId;
                ssp.ServiceId = service.ServiceId;
                ssp.HexColor = service.HexColor;

                lbi.Content = ssp;
                MyListBoxControl.Items.Add(lbi);
            }

Upvotes: 1

Views: 120

Answers (1)

iCollect.it Ltd
iCollect.it Ltd

Reputation: 93561

Rather than create the controls iteratively (which is slow for many controls) use binding to collections and a DataTemplate to show your display objects (SmallServicePanel etc.).

Once you have this in place it will use the virtualization feature of the ListBox (or choose another control that provides virtualisation). That means that the number of actual controls that exist will not be much more than what is displayed and performance will take a leap forward.

Let us know how you go.

Upvotes: 2

Related Questions