2011-02-01 82 views
3

我有一个包含四个列,在这我动态添加项目作为一个ListView:ListView项目排序?

ListViewItem lvi = new ListViewItem(); 
lvi.Background = ... color you want ... ; 
lvi.Content = new {Server = "test1", .... }; 
listViewResult.Items.Add(lvi); 

现在我想排序上perticular栏中单击此动态生成的ListView。 我该如何做到这一点?

回答

0

我发现文章here解释了自定义排序。

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.