Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 可观察到的分类收集<&燃气轮机;人工_C#_.net_Windows Phone 7_Windows Phone 7.1_Observablecollection - Fatal编程技术网

C# 可观察到的分类收集<&燃气轮机;人工

C# 可观察到的分类收集<&燃气轮机;人工,c#,.net,windows-phone-7,windows-phone-7.1,observablecollection,C#,.net,Windows Phone 7,Windows Phone 7.1,Observablecollection,在我的班级中,我有以下可观察的集合,我在Windows phone 7应用程序中使用该集合将数据绑定到我的列表框 public ObservableCollection<CustomClass> myList = new ObservableCollection<CustomClass>(); 公共ObservableCollection myList=新ObservableCollection(); 我的自定义类 公共类CustomClass { 公共字符串Id{g

在我的班级中,我有以下
可观察的集合
,我在
Windows phone 7
应用程序中使用该集合将数据绑定到我的
列表框

public ObservableCollection<CustomClass> myList = new ObservableCollection<CustomClass>();
公共ObservableCollection myList=新ObservableCollection(); 我的自定义类
公共类CustomClass
{
公共字符串Id{get;set;}
公共字符串名称{get;set;}
公共字符串EventName{get;set;}
获取公共字符串EventDate
{
返回事件日期;
}
设置
{
if(值!=null)
{
DateTime eventDate=DateTime.Parse(值);
int today=DateTime.Now.Day;
如果(eventDate.Day=今天-2)
{
如果(eventDate.Day==今天)
EventDate=“今天”;
else if(eventDate.Day==(今天+1))
EventDate=“明天”;
else if(eventDate.Day==(今天-1))
EventDate=“昨天”;
else if(eventDate.Day>=(今天-2))
EventDate=“刚刚过去”;
}
其他的
{
EventDate=值;
}
}
}
}
现在我想根据
EventDate

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

  • 刚刚过去
  • 昨天
  • 明天
  • 今天
  • 日期//格式“mm/dd”
  • 自定义集合只能按照上述顺序进行排序

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

    有可能吗???

    看看这里

    下面是一些得体的答案。

    看这里


    下面是一些不错的答案。

    您始终可以将以下子类化:

    /// <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));
            }
        }
    }
    
    //
    ///表示动态数据集合,该集合在添加、删除项目或刷新整个列表时提供通知并允许排序。
    /// 
    ///集合中元素的类型。
    公共类SortableObservableCollection:ObservableCollection
    {
    /// 
    ///根据键按升序对集合的项进行排序。
    /// 
    ///由返回的密钥的类型。
    ///从项中提取键的函数。
    公共无效排序(Func键选择器)
    {
    内部排序(Items.OrderBy(keySelector));
    }
    /// 
    ///根据键按升序对集合的项进行排序。
    /// 
    ///由返回的密钥的类型。
    ///从项中提取键的函数。
    ///一个比较键的方法。
    公共无效排序(Func键选择器、IComparer比较器)
    {
    内部排序(Items.OrderBy(keySelector,comparer));
    }
    /// 
    ///移动集合中的项目,使其顺序与提供的项目相同。
    /// 
    ///用于提供项目订单的。
    私有void InternalSort(IEnumerable sortedItems)
    {
    var sortedItemsList=sortedItems.ToList();
    foreach(sortedItemsList中的var项)
    {
    移动(IndexOf(item),sortedItemsList.IndexOf(item));
    }
    }
    }
    
    然后使用lambda表达式进行排序

    ((SortableObservableCollection<CustomClass>)MyList).Sort(s => s.EventDate);
    
    ((SortableObservableCollection)MyList).Sort(s=>s.EventDate);
    
    您始终可以子类化:

    /// <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));
            }
        }
    }
    
    //
    ///表示动态数据集合,该集合在添加、删除项目或刷新整个列表时提供通知并允许排序。
    /// 
    ///集合中元素的类型。
    公共类SortableObservableCollection:ObservableCollection
    {
    /// 
    ///根据键按升序对集合的项进行排序。
    /// 
    ///由返回的密钥的类型。
    ///从项中提取键的函数。
    公共无效排序(Func键选择器)
    {
    内部排序(Items.OrderBy(keySelector));
    }
    /// 
    ///根据键按升序对集合的项进行排序。
    /// 
    ///由返回的密钥的类型。
    ///从项中提取键的函数。
    ///一个比较键的方法。
    公共无效排序(Func键选择器、IComparer比较器)
    {
    内部排序(Items.OrderBy(keySelector,comparer));
    }
    /// 
    ///移动集合中的项目,使其顺序与提供的项目相同。
    /// 
    ///用于提供项目订单的。
    私有void InternalSort(IEnumerable sortedItems)
    {
    var sortedItemsList=sortedItems.ToList();
    foreach(sortedItemsList中的var项)
    {
    移动(IndexOf(item),sortedItemsList.IndexOf(item));
    }
    }
    }
    
    然后使用lambda表达式进行排序

    ((SortableObservableCollection<CustomClass>)MyList).Sort(s => s.EventDate);
    
    ((SortableObservableCollection)MyList).Sort(s=>s.EventDate);
    
    由于CustomClass没有实现INotifyPropertyChange,我假设您只需要在插入时进行排序(添加到集合时)。所以IMHO,最简单的方法(类似于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);
        }
    }
    
    公共类比较ObservableCollection:ObservableCollection
    其中T:i可比较
    {
    受保护的覆盖无效插入项(int索引,T项)
    {
    int i=0;
    bool-found=false;
    对于(i=0;i
    然后,您只需在CustomClass上实现
    IComparable
    ,如下所示:

    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);
        }
    }
    
    public类CustomClass:i可比较
    {
    公共字符串Id{get;set;}
    公共字符串名称{get;set;}
    公共字符串EventName{get;set;}
    公共字符串EventDate{get
    {
    返回事件日期;
    }
    设置
    {
    if(值!=null)
    {
    DateTime eventDate=DateTime.Parse(值);
    int