Вопрос

There is a ListBox with huge amount of ItemSource(Observablecollection, moore than 400000, include picture, string, and so on) in my WPF work, and ever time I run it, it took many time and space to load the ListBox. Is there any good solution to deal with it?

Это было полезно?

Решение

Just dealing with the UI- and WPF-specific bit of your problem: You need to virtualize your ListBox, which means that not the complete 'ItemsSource' is loaded into the control but only the portion that is being displayed currently (plus a bit to be prepared when the user scrolls down). The WPF ListBox supports virtualization out of the box, using 'VirtualizingStackPanel' attached properties.

Here is a snippet that shows the usage in principle:

<ListBox ItemsSource="{Binding Items}"
         VirtualizingStackPanel.IsVirtualizing="True" 
         VirtualizingStackPanel.VirtualizationMode="Recycling">
</ListBox>

If you do some research on this topic, you'll find lots of resources which will guide you how to properly implement virtualization.

That being said, the above only deals with the UI rendering bit of the performance problem. The list of items which you use as 'ItemsSource' will still be loaded completely behind the scenes as this has nothing to do with WPF. With 400k of datasets, including images (say 500k each), you will require more memory than any standard machine has available. Actually I'm surprised that it's working at all, but that greatly depends on the image size. Anyway, your approach to load this amount of data into the memory is far from scalable. Think about a redesign here, load the bits you need at a time and display them using virtualization. No user will ever work on half a million of datasets at a time...

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top