views:

351

answers:

2

I have a WPF ListBox control (view code) and I am keeping maybe like 100-200 items in it. Every time the ObservableCollection it is bound to changes though it takes it a split second to update and it freezes the whole UI. Is there a way to add elements incrementally or something I can do to improve the performance of this control?

A: 

Try setting VirtualizingStackPanel.IsVirtualizing="True" on your ListBox - MSDN Documentation. Also see this blog post I came across. I haven't tried it personally, but it seems like a good place to start. Good luck!

Pwninstein
A: 

Try something where (PanelList is a ListBox or something);

new Task(delegate {
   foreach (var info in new DirectoryInfo("C:\\windows\\system32").EnumerateFiles()) {
       PanelList.Dispatcher.Invoke(DispatcherPriority.Background, (Action)delegate {
          PanelList.Items.Add(info);
       });
       Thread.Sleep(0);
   }
}).Start();

You want to run a background task and update a UI control incrementally through Dispatcher.Invoke, make sure to set your priority relativly low, and I always throw a sleep in just for fun (voluntarially context swap), also you should be checking if your current task has been canceled...

Oh ya, this is not so much a performance improvement as precieved performance and UI responsiveness.

RandomNickName42