0

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

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;
        }
    }
}
}

现在我想myList根据数据中的数据进行排序EventDate

在所有情况下,EventDate 中的数据将是以下之一

  1. 刚刚过去
  2. 昨天
  3. 明天
  4. 今天
  5. 日期 //格式“MMM/dd”

自定义 Collection 只能按照上面的顺序排序

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

是否可以??

4

3 回答 3

2

由于您的 CustomClass 没有实现 INotifyPropertyChange,我假设您只需在插入时进行排序(添加到集合时)。所以恕我直言,最简单的事情(类似于 Randolf Rincón-Fadul 的解决方案)是子类化,然后覆盖 Add 方法。

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);
    }
}

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

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);
    }
}
于 2012-06-26T09:26:29.007 回答
1

看过这里??

http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/5909dbcc-9a9f-4260-bc36-de4aa9bbd383/

底部有一些不错的答案。

于 2012-06-25T14:33:21.510 回答
1

你总是可以子类化:

/// <summary>
/// Represents a dynamic data collection that provides notifications when items get added, removed, or when the whole list is refreshed and allows sorting.
/// </summary>
/// <typeparam name="T">The type of elements in the collection.</typeparam>
public class SortableObservableCollection<T> : ObservableCollection<T>
{
    /// <summary>
    /// Sorts the items of the collection in ascending order according to a key.
    /// </summary>
    /// <typeparam name="TKey">The type of the key returned by <paramref name="keySelector"/>.</typeparam>
    /// <param name="keySelector">A function to extract a key from an item.</param>
    public void Sort<TKey>(Func<T, TKey> keySelector)
    {
        InternalSort(Items.OrderBy(keySelector));
    }

    /// <summary>
    /// Sorts the items of the collection in ascending order according to a key.
    /// </summary>
    /// <typeparam name="TKey">The type of the key returned by <paramref name="keySelector"/>.</typeparam>
    /// <param name="keySelector">A function to extract a key from an item.</param>
    /// <param name="comparer">An <see cref="IComparer{T}"/> to compare keys.</param>
    public void Sort<TKey>(Func<T, TKey> keySelector, IComparer<TKey> comparer)
    {
        InternalSort(Items.OrderBy(keySelector, comparer));
    }

    /// <summary>
    /// Moves the items of the collection so that their orders are the same as those of the items provided.
    /// </summary>
    /// <param name="sortedItems">An <see cref="IEnumerable{T}"/> to provide item orders.</param>
    private void InternalSort(IEnumerable<T> sortedItems)
    {
        var sortedItemsList = sortedItems.ToList();

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

然后使用 lambda 表达式排序

((SortableObservableCollection<CustomClass>)MyList).Sort(s => s.EventDate);
于 2012-06-25T14:41:53.123 回答