列表ListBox、ListView、GridView 排序

简介: 列表排序 1.使用控件默认排序方式(推荐) ListControl.Items.SortDescriptions.Clear(); ListControl.Items.SortDescriptions.

列表排序

1.使用控件默认排序方式(推荐)

    ListControl.Items.SortDescriptions.Clear();
    ListControl.Items.SortDescriptions.Add(new SortDescription("IsGroup", ListSortDirection.Descending));
    ListControl.Items.SortDescriptions.Add(new SortDescription(_sortingField?? "UpdateTime", _sortingDirection));
    ListControl.Items.Refresh();

2.使用CollectionView排序

var collectionView = CollectionViewSource.GetDefaultView(ListControl.ItemsSource);
if (collectionView != null)
{
    collectionView.SortDescriptions.Clear();
    collectionView.SortDescriptions.Add(new SortDescription("IsGroup", ListSortDirection.Descending));
    collectionView.SortDescriptions.Add(new SortDescription(_sortingField, sortingDirection));
    collectionView.Refresh();
}

2.自定义SortableObservableCollection

 public class SortableObservableCollection<T> : ObservableCollection<T>
    {
        public SortableObservableCollection()
        {
        }

        public SortableObservableCollection(List<T> list)
            : base(list)
        {
        }

        public SortableObservableCollection(IEnumerable<T> collection)
            : base(collection)
        {
        }

        public void Sort<TKey>(Func<T, TKey> keySelector, System.ComponentModel.ListSortDirection direction)
        {
            switch (direction)
            {
                case System.ComponentModel.ListSortDirection.Ascending:
                {
                    ApplySort(Items.OrderBy(keySelector));
                    break;
                }
                case System.ComponentModel.ListSortDirection.Descending:
                {
                    ApplySort(Items.OrderByDescending(keySelector));
                    break;
                }
            }
        }

        public void Sort<TKey>(Func<T, TKey> keySelector, IComparer<TKey> comparer)
        {
            ApplySort(Items.OrderBy(keySelector, comparer));
        }

        private void ApplySort(IEnumerable<T> sortedItems)
        {
            var sortedItemsList = sortedItems.ToList();

            foreach (var item in sortedItemsList)
            {
                Move(IndexOf(item), sortedItemsList.IndexOf(item));
            }
        }
    }

添加列表属性,并绑定到控件

        public SortableObservableCollection<CoursewareListItem> Items
        {
            get { return _items; }
            set
            {
                _items = value;
                RaisePropertyChanged("Items");
            }
        }

在排序触发时,添加

viewModel.Items.Sort(item => item.UpdateTime, sortingDirection);

 

值得注意的是:异步线程高度当前主UI线程时,ObservableCollection列表是不支持重新排序的。

关键字:列表排序,CollectionViewSource,ObservableCollection,异步线程对列表排序

本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须在文章页面给出原文连接,否则保留追究法律责任的权利。
目录
相关文章
|
9月前
|
监控 Java 应用服务中间件
tomcat相关概念与部署tomcat多实例-zabbix监控(docker部署)
通过上述步骤,您可以在Ubuntu系统上成功编译并安装OpenCV 4.8。这种方法不仅使您能够定制OpenCV的功能,还可以优化性能以满足特定需求。确保按照每一步进行操作,以避免常见的编译问题。
208 25
|
前端开发 Linux Shell
|
机器学习/深度学习 算法 Oracle
量子计算与量子密码(入门级-少图版)(4)
量子计算与量子密码(入门级-少图版)(4)
477 0
|
算法 前端开发 Linux
【常用技巧】C++ STL容器操作:6种常用场景算法
STL在Linux C++中使用的非常普遍,掌握并合适的使用各种容器至关重要!
244 10
|
敏捷开发
财富500强公司伊瑞保险是如何培养变革型人才的
财富500强公司伊瑞保险是如何培养变革型人才的
|
应用服务中间件 Apache
tomcat http 404
tomcat http 404
256 0
|
机器学习/深度学习 数据可视化 算法
模型推理加速系列 | 06: 基于 resnet18 评测各加速方案
天这篇文章以resnet18模型为例,对比Pytorch、ONNX、TorchScript、TensorRT模型格式在不同硬件(包括CPU和GPU)上的inference性能。
【算法题解】Codeforces Round #817 (Div. 4)题解
【算法题解】Codeforces Round #817 (Div. 4)题解
【算法题解】Codeforces Round #817 (Div. 4)题解
|
前端开发 JavaScript
mvvm全网最详细讲解
mvvm全网最详细讲解
155 0
|
JSON JavaScript 小程序
开源Vue表格组件,表格插件源码
开源Vue表格组件,表格插件源码
301 0
开源Vue表格组件,表格插件源码