A Sortable ObservableCollection for WPF

本文介绍了一个名为SortableObservableCollection的自定义类,它允许您直接在ObservableCollection上进行排序操作,无需重新绑定到UI,从而简化了应用程序中的数据排序流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

You probably have had the need to sort an ObservableCollection at some point in one of your applications by either ascending or descending order.  Of course, you can always use the ObservableCollection.OrderBy and ObservableCollection.OrderByDescending, but these methods just return a new collection of IOrderedEnumerable, which forces you have to rebind the DataContext/ItemsSource in your UI, which could be a pain.  So instead, wouldn’t it be nice to have an ObservableCollection that you could call a Sort method on, give it a lambda to sort on and a direction, then have it perform the sort without having to rebind to the UI.  Well, it is not a difficult as you may think.  So lets go ahead and write one.


public class SortableObservableCollection<T> : ObservableCollection<T>
{
  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));
     }
  }
}

Now we can create a new SortableObservableCollection<T> and sort it either direction without having to return a new IOrderedEnumerable and rebind the DataContext/ItemsSource.

//sort ascending
MySortableList.Sort(x => x.Name, ListSortDirection.Ascending);

//sort descending
MySortableList.Sort(x => x.Name, ListSortDirection.Descending);

When you sort your collection, the UI is notified that something has changed and updates itself automatically.

=======================================================================================

或者在OnWindowLoaded()函数中直接使用SortDirection,代码中是默认的降序排序

            string header = "HeaderName";
 	    ListSortDirection direction;            
            direction = ListSortDirection.Descending;
            ICollectionView dataView = CollectionViewSource.GetDefaultView(lv.ItemsSource);
            dataView.SortDescriptions.Clear();
            SortDescription sd = new SortDescription(header, direction);
            dataView.SortDescriptions.Add(sd);
            dataView.Refresh();



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值