.net - 在 TextBox 中以 d/mm 格式输入日期

标签 .net windows-8 .net-4.5 cultureinfo

我有一个 TextBox 绑定(bind)到一个可为空的 DateTime 属性。

我在澳大利亚,所以我希望以 d/mm/yyyy 格式显示日期。

在我的 Windows 7 框中,我可以输入 d/mm 格式的日期,并且它已正确转换(例如,1/11 转换为 11 月 1 日,13/1 转换为 1 月 13 日等)

在我的 Windows 8 机器上,相同的输入被解释为美国格式,因此 1/11 转换为 1 月 11 日,而 13/1 失败(因为没有第 13 个月)。

两台计算机都设置为使用澳大利亚格式,我在 Application.StartUp 事件中有以下代码:

FrameworkElement.LanguageProperty.OverrideMetadata(GetType(FrameworkElement), New FrameworkPropertyMetadata(XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.IetfLanguageTag)))

Binding 的 StringFormat 设置为 d/MM/yyyy,并且我通过将 TextBlock 绑定(bind)到 StringFormat 设置为 D 的相同属性(长日期格式,它给出的值如“星期三, 2012 年 1 月 11 日')。

有没有人有任何想法?

更新:进一步的调查(见下面的评论)表明这似乎是一个问题,与 Windows 7 相比,Windows 8 上的 en-AU 文化不同,这意味着它在 Windows 8 上以 mm/dd 格式解释像“1/11”这样的日期,而在 Windows 7 上,它以 dd/mm 格式解释它们,这是我在使用 en-AU 文化时所期望的。

最佳答案

由于不推荐使用 IetfLanguageTag,您是否考虑过使用 Name 属性?

http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo.ietflanguagetag.aspx :

The format of an IETF language tag is similar to the culture name returned by the Name property, but does not identify a culture uniquely. That is, different cultures share the same IETF language tag if those cultures have identical linguistic characteristics.

关于.net - 在 TextBox 中以 d/mm 格式输入日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13214749/

相关文章:

c# - COM Interop,客户端在进程外 COM 中找不到接口(interface)

javascript - ASPxMenu - 单击而不是悬停打开子菜单 (dx :ASPxMenu)

c - 如何在 Windows 8 上安装 C 编译器?我不想使用 IDE

c# - 数据绑定(bind)和 WPF

c# - Task<WebResponse>.Wait 永远持续

mysql - 频繁项集生成的数据库和应用程序设计

.net - GoogleWebAuthorizationBroker 不会自动从刷新 token 返回新的访问 token

c# - 对于匿名处理程序来说,一种语法是更好的选择吗?

sql-server - SQL Server 2012 默认实例的连接字符串是什么?

.net - 枚举作为 Entity Framework 5 中的键在多对多连接上抛出错误