我有一个包含四列的 ListView,我在其中动态添加项目:
ListViewItem lvi = new ListViewItem();
lvi.Background = ... color you want ... ;
lvi.Content = new {Server = "test1", .... };
listViewResult.Items.Add(lvi);
现在我想在特定列单击时对这个动态生成的 ListView 进行排序。 我怎样才能做到这一点?
最佳答案
我找到文章 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.
关于wpf - ListView 项目排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4861065/