wpf - ListView 项目排序?

标签 wpf sorting listview .net-4.0

我有一个包含四列的 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/

相关文章:

javascript - 有没有办法随机打乱数组,以便数组每次都可以不同地出现?

Android ListView 不通过方向更改保留值

android - 更改 ListView 项的文本颜色时出现 NullPointerException

wpf - xaml "local:"是什么?

c# - 如何查询正在运行的线程

datetime - 如何在 extjs4 网格中设置默认排序列并更改日期格式?

c# - 如何隐藏有关缺少列详细信息的行的 TreeListView 错误消息?

c# - 如何在包含文件的网格中显示文件上下文菜单

wpf - 将 WPF UserControl 中的内容绑定(bind)到其属性的不同方法的优点/缺点是什么?

java - 排序父子关系