ListView项目排序?

3

我有一个包含四列的ListView,我正在动态添加项目,如下:

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

现在我想在特定列点击时动态地对生成的ListView进行排序。我该如何实现?

1个回答

0
我找到了这篇文章在这里,它解释了自定义排序的方法。
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.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接