排序的ObservableCollection<串GT; C# [英] Sort ObservableCollection<string> C#

查看:404
本文介绍了排序的ObservableCollection<串GT; C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的ObservableCollection<串GT; 。我需要的排序这个字母。

I have below ObservableCollection<string>. I need to sort this alphabetically.

private ObservableCollection<string> _animals = new ObservableCollection<string>
    {
        "Cat", "Dog", "Bear", "Lion", "Mouse",
        "Horse", "Rat", "Elephant", "Kangaroo", "Lizard", 
        "Snake", "Frog", "Fish", "Butterfly", "Human", 
        "Cow", "Bumble Bee"
    };

我试过 _animals.OrderByDescending 。但我不知道如何正确使用它。

I tried _animals.OrderByDescending. but i don't know how to use it correctly.

_animals.OrderByDescending(a => a.<what_is_here_?>);

我怎样才能做到这一点?

How can i do this ?

推荐答案

基本上,如果有必要显示一个有序集合,请考虑使用 Col​​lectionViewSource 类:分配(绑定),其来源属性的源集合 - 的一个实例的ObservableCollection&LT; T&GT;

Introduction

Basically, if there is a need to display a sorted collection, please consider using the CollectionViewSource class: assign ("bind") its Source property to the source collection — an instance of the ObservableCollection<T> class.

的想法是,<一href=\"http://msdn.microsoft.com/en-us/library/system.windows.data.collectionviewsource.getdefaultview%28v=vs.110%29.aspx\"><$c$c>CollectionViewSource类提供了的CollectionView 类的一个实例。这是一种原始(源)收集的投影,但随着应用的排序,过滤等。

The idea is that CollectionViewSource class provides an instance of the CollectionView class. This is kind of "projection" of the original (source) collection, but with applied sorting, filtering, etc.

参考文献:

  • How to: Sort and Group Data Using a View in XAML.
  • WPF’s CollectionViewSource.

WPF 4.5引入了直播整形功能 Col​​lectionViewSource

WPF 4.5 introduces "Live Shaping" feature for CollectionViewSource.

参考文献:

  • WPF 4.5 New Feature: Live Shaping.
  • CollectionViewSource.IsLiveSorting Property.
  • Repositioning data as the data's values change (Live shaping).

如果仍有需要排序的的ObservableCollection&LT的实例; T&GT; 类,这里是如何可以做到。
的ObservableCollection&LT; T&GT; 类本身不具有排序方法。但是,收集可以重新创建有项目进行排序:

If there still a need to sort an instance of the ObservableCollection<T> class, here is how it can be done. The ObservableCollection<T> class itself does not have sort method. But, the collection could be re-created to have items sorted:

// Animals property setter must raise "property changed" event to notify binding clients.
// See INotifyPropertyChanged interface for details.
Animals = new ObservableCollection<string>
    {
        "Cat", "Dog", "Bear", "Lion", "Mouse",
        "Horse", "Rat", "Elephant", "Kangaroo",
        "Lizard", "Snake", "Frog", "Fish",
        "Butterfly", "Human", "Cow", "Bumble Bee"
    };
...
Animals = new ObservableCollection<string>(Animals.OrderBy(i => i));

的更多细节

请注意,的OrderBy() OrderByDescending()方法(如其他LINQ的扩展方法)<强>不要修改源集合!相反,他们的创建一个新的序列(即类,它实现的IEnumerable℃的新实例; T&GT; 接口)。因此,有必要重新创建集合

Additional details

Please note that OrderBy() and OrderByDescending() methods (as other LINQ–extension methods) do not modify the source collection! They instead create a new sequence (i.e. a new instance of the class that implements IEnumerable<T> interface). Thus, it is necessary to re-create the collection.

这篇关于排序的ObservableCollection&LT;串GT; C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆