Jens
Jens

Reputation: 25563

Sorting an ItemsControl without re-constructing item templates?

I am allowing the users to sort items shown by an ItemsControl using something similar to

var listCollectionView = CollectionViewSource.GetDefaultView(myItemsControl.ItemsSource);
listCollectionView.CustomSort = mySortComparer;
// Alternative, same Problem: 
// listCollectionView.SortDescriptions.Add(new SortDescription(myProperty)); 

The ItemTemplate for my collection is a little complicated and creating all the item representations requires a significant amount of time. I've found, using the profiler, that each sort re-creates every item in the ItemsControl, instead of using the old items and just moving them.

Is there a way to prevent this?

Upvotes: 0

Views: 54

Answers (1)

vadim
vadim

Reputation: 176

Try using VirtualizingStackPanel as your ItemsControl's items panel if you aren't already. And also set VirtualizingStackPanel.VirtualizationMode attached property to Recycling on the ItemsControl.

Upvotes: 1

Related Questions