In my first part of performance tuning techniques, I’ve reviewed some general approaches. Now I want to show you how to fine tune your ListView for sorting and displaying HUGE amount of rows. In this demo, I will show you that by using a custom sorter and virtualizingstackpanel, your sorting speed is about 100 times faster!
VirtualizingStackPanel.IsVirtualizing=”True”
First you need to specify the above property to true in your ListView, (this is the default value for ListView in WPF).
Then next, you need to use custom sorter, instead of SortDescriptions as described in my earlier blog. The key is using the CustomSort property of ListCollectionView:
ListCollectionView view = (ListCollectionView)CollectionViewSource.GetDefaultView(myListView.ItemsSource);
Then in your ColumnHeader click event handler, you add something like the following:
view.CustomSort = sorter;
myListView.Items.Refresh();
Where sorter is a custom class you implement the IComparer interface.
Here is the demo of this custom (much faster!) sort program to sort 200,000 rows in a virtualized ListView in WPF:
In the demo, you see the custom sort takes about only 1.6 – 2 seconds to sort. I tried this 200,000 rows with SortDescriptions, and it takes about 3 minutes (~ 167 – 180 seconds) to sort this many rows. This is tested on both the .Net Framework 3.0 and the just released .Net Framework 3.5 Beta 2. Now you can enjoy a much more scalable ListView in WPF.
[Update 8/1/2007]
I’ve uploaded the project source code to demonstrate this sorting technique. You can get it here. You’ll need Visual Studio 2008 Beta 2 to open the solution file (or project file).
[Update 8/3/2007]
A modified version of the sample to support sorting multiple columns is posted here. You’ll need Visual Studio 2008 Beta 2 to open the solution file (or project file).
[Updated: 1/16/2009]
The new source code link is updated at:
Posted by ligao101
Posted by ligao101