2012-06-22 72 views
4

我的WPF应用程序将其数据使用XMLDataProvider。对于每个列出的项目,XML文件具有将字符串日期转换为使用SortDescription的日期

<RELEASEDATE>dd/mm/yyyy</RELEASEDATE> 

。我正在使用应用排序应用中的数据

Listbox1.Items.SortDescriptions.Add(new SortDescription("RELEASEDATE", ListSortDirection.Descending)); 

由于日期被视为字符串,因此结果不是预期的结果。

什么是最优雅的方式呢?我可以以某种方式转换为内嵌日期吗?

回答

2

您必须实现自己的IComparer:

class DateTimeComparer : IComparer 
    { 
     public int Compare(object x, object y) 
     { 
      //To Do : Implement DataTime Comparering 
     } 
    } 

现在IComparer实现分配到集合的ListCollectionView.CustomSort:

ListCollectionView view = new ListCollectionView(ListBox.Items); 

view.CustomSort = new DateTimeComparer(); 

See similar Question

+0

谢谢你,我实现这个尽快! –