views:

140

answers:

2

I am developing a program that should be able to display long (up to 500 items) lists of data that need to be resorted when their contents change.

Essentially, I have a viewmodel with an observable collection that contains classes with observable data bound to the gui, which is displayed in a ListView.

The data must be sorted, but the data may also change at any time, and the list needs to be resorted each time.

What is the best mechanism / metaphor for displaying and keeping the whole thing resorted without locking up the GUI? I have a solution using NotifyCollectionChangedEventArgs and some sorting functions, but its SLOW - I'm assuming it resorts and rebuilds the entire GUI each time a data element is changed.

A: 

Is VirtualizingStackPanel any help?

Steve Cooper
I wish it was, but the sorting mechanism doesn't work with it very well
bluebit
+2  A: 

Have a look at the CollectionView/CollectionViewSource classes. These classes sit 'between' your ObservableCollection and WPF's data binding logic in order to perform operations like sorting, filtering, etc.

I suspect that writing your own sorting functions off of NotifyCollectionChanged is the source of your performance issues; it depends on your precise implementation, but there probably are a flurry of CollectionChanged events being raised as you sort the list, which results in WPF re-binding the collection each iteration of your sort routine... which would be incredibly slow, as you described.

CollectionView and CollectionViewSource don't affect the source collection, just the view of the collection displayed on the UI, so you should see a significant speedup over what you are doing now. When Microsoft's DataGrid control was released, they had demos of it displaying and sorting millions of rows - and it's using precisely these classes to perform its sorts. You really shouldn't be seeing performance problems on 500 rows.

Finally, the difference between CollectionView and CollectionViewSource are where they are designed to be used; CollectionView is used when you are working in C#, CollectionViewSource when you're doing it from XAML. You may also want to have a look at this article for a quick overview of these classes.

Nicholas Armstrong