排序列表与的ObservableCollection [英] Sorting List vs. ObservableCollection

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

问题描述

我发现,我做WPF错和令人沮丧的,必须大修我的代码很多



我怎么能转换成如下:



 公共静态类SortName 
{
公共静态INT比较(某甲,某乙)
{
返回a.Name.CompareTo(b.Name);
}

}

和我这样称呼它:

  list.Sort(SortName.Compare); 



为的ObservableCollection要求的格式。而且我会怎么称呼它。到目前为止,我已经试过此基础上我读到的这里

 类ObservableCollectionSortName< T> :与的ObservableCollection LT; T> 
{
公众诠释比较(一人,人B)
{
返回a.Name.CompareTo(b.Name);
}
}


解决方案

的观察的集合没有实现排序,原因很简单,每一个项目从一个位置移到列表中的另一个集合时引发一个事件。 。这将是伟大的看着行动的排序算法的动画,但它那种吸了,你知道,分类



有两种方法可以做到这一点;他们非常相似,都通过排序他们观察到的集合,例如外项目启动如果 _Fruits 的ObservableCollection<水果GT; ,以及您所定义的的IComparer<水果GT ; 的排序,你会怎么做:

  VAR分类= _Fruits.OrderBy(X = &X的催化剂,新FruitComparer()); 

这将创建一个新的的IEnumerable<水果GT; ,当你迭代它,将在新秩序中的对象。有两件事情你可以用这个做的。



一个是创建一个新的集合,取代旧的,并且强迫任何项目控制(S)在UI重新绑定到它:

  _Fruits =新的ObservableCollection<水果GT;(排序); 
OnPropertyChanged(水果);



(这里假设你的类实现 INotifyPropertyChanged的在通常的方式)



另一种方法是创建一个新的排序列表,然后用它来的移动的您的收藏中的项目:

  INT I = 0; 
的foreach(水果f由于排序)
{
_Fruits.MoveItem(_Fruits.IndexOf(F),I);
I ++;
}



第二种方法是东西,如果我有一个非常严重的我只尝试承诺不重新绑定的物品控制,因为它会提高一吨集合改变的事件。


I have found out that I am "doing WPF wrong" and frustratingly must overhaul alot of my code.

How could I convert the following:

public static class SortName 
    {
       public static  int Compare(Person a, Person b)
        {
            return a.Name.CompareTo(b.Name);
        }

    }

and I call it like:

list.Sort(SortName.Compare);

to the format required for ObservableCollection. And how would I call it. So far i've tried this following based on what I read here

class ObservableCollectionSortName<T> : ObservableCollection<T> 
{
    public int Compare (Person a, Person b)
    {
        return a.Name.CompareTo(b.Name);
    }
}

解决方案

The observable collection doesn't implement sorting, for the simple reason that every time an item moves from one location in the list to another the collection raises an event. That would be great for watching animations of the sort algorithm in action, but it would sort of suck for, you know, sorting.

There are two ways to do this; they're very similar, and both start by sorting the items outside their observable collection, e.g. if _Fruits is an ObservableCollection<Fruit>, and you've defined an IComparer<Fruit> for the sort, you'd do:

var sorted = _Fruits.OrderBy(x => x, new FruitComparer());

That creates a new IEnumerable<Fruit> that, when you iterate over it, will have the objects in the new order. There are two things you can do with this.

One is to create a new collection, replace the old one, and force any items control(s) in the UI to rebind to it:

_Fruits = new ObservableCollection<Fruit>(sorted);
OnPropertyChanged("Fruits");

(This assumes that your class implements INotifyPropertyChanged in the usual way.)

The other is to create a new sorted list, and then use it to move the items in your collection:

int i = 0;
foreach (Fruit f in sorted)
{
   _Fruits.MoveItem(_Fruits.IndexOf(f), i);
   i++;
}

The second approach is something I'd only try if I had a really serious commitment to not rebinding the items controls, because it's going to raise a ton of collection-changed events.

这篇关于排序列表与的ObservableCollection的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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