将字符串日期转换为使用SortDescription的日期 [英] Converting a string date to a date when using a SortDescription

查看:173
本文介绍了将字符串日期转换为使用SortDescription的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的WPF应用程序使用 XMLDataProvider 作为其数据。 XML文件有一个

My WPF app using an XMLDataProvider for its data. The XML file has a

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

为每个列出的项目。我正在使用

for each of the listed items. I'm sorting the data in the app using a

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

结果不是预期的,因为日期被视为一个字符串。

The results aren't what expected as the date is treated as a string.

这是最优雅的方式?我可以以某种方式转换为内联日期吗?

What is the most elegant way around this? Can I somehow convert to a date inline?

推荐答案

您必须实现自己的IComparer:

You have to implement own IComparer:

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

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

and now assign the IComparer implementation to the collection's ListCollectionView.CustomSort:

 ListCollectionView view = new ListCollectionView(ListBox.Items);

 view.CustomSort = new DateTimeComparer();

查看相似的问题

这篇关于将字符串日期转换为使用SortDescription的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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