这个问题可能看起来像这样,但实现方式不同。我将该示例仅用于我的新实现
我在课堂上关注 ObservableCollection,我在 Windows phone 7 应用程序中使用该集合将数据绑定到我的列表框
public ObservableCollection<CustomClass> myList = new ObservableCollection<CustomClass>();
我尝试了以下方式,但排序不正确
我的课
public class CustomClass : IComparable<CustomClass>
{
public string Id { get; set; }
public string Name { get; set; }
public string CreatedDate get{ get; set; }
public int CompareTo(CustomClass other)
{
var compareDate1 = DateTime.Parse(CreatedDate);
var compareDate2 = DateTime.Parse(other.CreatedDate);
return compareDate2.CompareTo(compareDate1);
}
}
子类
public class ComparingObservableCollection<T> : ObservableCollection<T>
where T : IComparable<T>
{
protected override void InsertItem(int index, T item)
{
if (!Items.Contains<T>(item))
{
try
{
var bigger = Items.First<T>(F => F.CompareTo(item) > 0);
index = Items.IndexOf(bigger);
}
catch
{
index = Items.Count;
}
finally
{
base.InsertItem(index, item);
}
}
}
}
问题不在于逻辑,而在于输入日期,我正在查询下一个 15 天 createddates 而不检查下面的代码是否有效,但需要处理 dec/jan 月份的日子
更新 我的自定义类
public class CustomClass : IComparable<CustomClass>
{
public string Id { get; set; }
public string Name { get; set; }
public DateTime Created { get; set; }
private string _CreatedDate;
public string CreatedDate
{
private get
{
return _CreatedDate;
}
set
{
Created = DateTime.Parse(value);
_CreatedDate = value;
}
}
public CustomClass(string id, string name, string created)
{
Id = id;
Name = name;
CreatedDate = created;
}
public int CompareTo(CustomClass other)
{
return CreateDate.Date.DayOfYear.CompareTo(other.CreateDate.Date.DayOfYear);
}
}