WPF绑定的StringFormat短日期字符串 [英] WPF Binding StringFormat Short Date String

查看:989
本文介绍了WPF绑定的StringFormat短日期字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在WPF使用短命名的日期字符串格式。

I would like to use Short Date named string format in WPF.

我想是这样的:

<TextBlock Text="{Binding Date, StringFormat='Short Date'}" />

如何做到这一点?

How to do this?

推荐答案

试试这个:

<TextBlock Text="{Binding PropertyPath, StringFormat=d}" />

这是文化敏感,需要.NET 3.5 SP1或更高版本。

which is culture sensitive and requires .NET 3.5 SP1 or above.

请注意:这是区分大小写的。 d是短日期格式说明而D是的长日期格式说明

NOTE: This is case sensitive. "d" is the short date format specifier while "D" is the long date format specifier.

有在 MSDN页标准日期和时间格式字符串字符串格式的完整列表和所有选项上的<一个更全面的解释href=\"http://blogs.msdn.com/b/vsdata/archive/2009/07/07/customize-format-of-datetime-string-in-wpf-and-winform-data-binding.aspx\">this MSDN博客帖子

但是,有一个问题与本 - 除非你的文​​化设置为正确的值自己总是输美国的日期格式

However, there is one gotcha with this - it always outputs the date in US format unless you set the culture to the correct value yourself.

如果您没有设置该属性,绑定引擎使用的语言绑定目标对象的属性。在XAML此默认为EN-US或继承了根元素的页面的值(或任何元素),如果已经明确设置。

If you do not set this property, the binding engine uses the Language property of the binding target object. In XAML this defaults to "en-US" or inherits the value from the root element (or any element) of the page, if one has been explicitly set.

<一个href=\"http://msdn.microsoft.com/en-us/library/system.windows.data.binding.converterculture(v=vs.110).aspx\">Source

要做到这一点的方法之一是在后面的(假设你已经设置线程的文化,正确的值)code:

One way to do this is in the code behind (assuming you've set the culture of the thread to the correct value):

this.Language = XmlLanguage.GetLanguage(Thread.CurrentThread.CurrentCulture.Name);

另一种方法是,设置转换器培养中的结合:

The other way is to set the converter culture in the binding:

<TextBlock Text="{Binding PropertyPath, StringFormat=d, ConverterCulture=en-GB}" />

虽然这不会让你本地化输出。

Though this doesn't allow you to localise the output.

这篇关于WPF绑定的StringFormat短日期字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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