对ObservableCollection<>进行排序手动地 [英] Sorting ObservableCollection<> manually

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

问题描述

我在课堂上关注ObservableCollection,我正在使用Windows phone 7应用程序中的Collection将数据绑定到我的listbox

I have following ObservableCollection in my class, I am binding data to my listbox using that Collection in my Windows phone 7 application

public ObservableCollection<CustomClass> myList = new ObservableCollection<CustomClass>();

我的自定义课程

public class CustomClass
{
public string Id { get; set; }        
public string Name { get; set; }        
public string EventName { get; set; }        
public string EventDate get
{
    return EventDate;
}
set
{
    if (value != null)
    {
        DateTime eventDate = DateTime.Parse(value);
        int today = DateTime.Now.Day;
        if (eventDate.Day <= today + 1 & eventDate.Day >= today - 2)
        {
            if (eventDate.Day == today)
            EventDate = "Today";
            else if (eventDate.Day == (today + 1))
            EventDate = "Tomorrow";
            else if (eventDate.Day == (today - 1))
            EventDate = "Yesterday";
            else if (eventDate.Day >= (today - 2))
            EventDate = "Just Passed";
        }
        else
        {
            EventDate = value;
        }
    }
}
}

现在我想根据EventDate

在所有情况下,EventDate中的数据均为以下其中之一

The Data in the EventDate will be one of the following in all the cases

  1. 刚刚通过
  2. 昨天
  3. 明天
  4. 今天
  5. 日期//格式化"MMM/dd"

自定义收藏只能按上述顺序排序

我从不同来源获取数据,因此在将数据绑定到集合中时无法进行排序

i am getting data from different sources so sorting is not possible at the time of binding data into the collection

有可能吗?

推荐答案

由于您的CustomClass没有实现INotifyPropertyChange,因此我假设您只需要在插入时进行排序(添加到集合中时).因此,恕我直言,最容易做的事情(类似于RandolfRincón-Fadul的解决方案)是子类化,然后重写Add方法.

Since your CustomClass doesn't implement INotifyPropertyChange, I'm assuming you'll only have to sort at insert (when adding to the collection). So IMHO, the easiest thing to do (similiar to Randolf Rincón-Fadul's solution) is to subclass, then override the Add method.

public class ComparingObservableCollection<T> : ObservableCollection<T>
     where T : IComparable<T>
{

    protected override void InsertItem(int index, T item)
    {
        int i = 0;
        bool found = false;
        for (i = 0; i < Items.Count; i++)
        {
            if (item.CompareTo(Items[i]) < 0) {
                found = true;
                break;
            }
        }

        if (!found) i = Count;

        base.InsertItem(i, item);
    }
}

然后您要做的就是像这样在CustomClass上实现IComparable<CustomClass>:

Then all you have to do is implement IComparable<CustomClass> on CustomClass like this:

public class CustomClass : IComparable<CustomClass>
{
public string Id { get; set; }        
public string Name { get; set; }        
public string EventName { get; set; }        
public string EventDate { get
{
    return EventDate;
}
set
{
    if (value != null)
    {
        DateTime eventDate = DateTime.Parse(value);
        int today = DateTime.Now.Day;
        if (eventDate.Day <= today + 1 & eventDate.Day >= today - 2)
        {
            if (eventDate.Day == today)
            EventDate = "Today";
            else if (eventDate.Day == (today + 1))
            EventDate = "Tomorrow";
            else if (eventDate.Day == (today - 1))
            EventDate = "Yesterday";
            else if (eventDate.Day >= (today - 2))
            EventDate = "Just Passed";
        }
        else
        {
            EventDate = value;
        }
    }
}
    private int Order { get {
       switch(EventDate) {
         case "Just Passed": return 1;
         case "Yesterday": return 2;
         case "Tomorrow": return 3;
         case "Today": return 4;
         default: return 5;
       }
    }
    }

    public int CompareTo(CustomClass other) {
       return this.Order.CompareTo(other.Order);
    }
}

这篇关于对ObservableCollection&lt;&gt;进行排序手动地的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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