WPF 绑定 StringFormat 短日期字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5046429/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 22:35:48  来源:igfitidea点击:

WPF Binding StringFormat Short Date String

wpfxamlbinding

提问by Tony

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

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

I tried something like:

我试过类似的东西:

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

How to do this?

这该怎么做?

回答by ChrisF

Try this:

尝试这个:

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

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

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

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

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

There's a full list of string format on the MSDN page on Standard Date and Time Format Stringsand a fuller explanation of all the options on this MSDN blog post

在标准日期和时间格式字符串MSDN 页面上有一个完整的字符串格式列表,以及这篇 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.

但是,这有一个问题 - 它总是以美国格式输出日期,除非您自己将文化设置为正确的值。

If you do not set this property, the binding engine uses the Languageproperty 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.

如果未设置此属性,则绑定引擎将使用绑定目标对象的Language属性。在 XAML 中,这默认为“en-US”或从页面的根元素(或任何元素)继承值(如果已显式设置)。

Source

来源

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.

尽管这不允许您本地化输出。

回答by else

Or use this for an English (or mix it up for custom) format:

或者将其用于英语(或将其混合用于自定义)格式:

StringFormat='{}{0:dd/MM/yyyy}'

回答by user7116

Use the StringFormatproperty (or ContentStringFormaton ContentControland its derivatives, e.g. Label).

使用StringFormat属性(或ContentStringFormatonContentControl及其衍生物,例如Label)。

<TextBlock Text="{Binding Date, StringFormat={}{0:d}}" />

Note the {}prior to the standard String.Formatpositional argument notation allows the braces to be escaped in the markup extension language.

请注意,{}标准String.Format位置参数符号之前的符号允许在标记扩展语言中对大括号进行转义。

回答by Telos

Some DateTime StringFormat samples I found useful. Lifted from C# Examples

我发现一些有用的 DateTime StringFormat 示例。从C# 示例中提取

DateTime dt = new DateTime(2008, 3, 9, 16, 5, 7, 123);

String.Format("{0:y yy yyy yyyy}", dt);  // "8 08 008 2008"   year
String.Format("{0:M MM MMM MMMM}", dt);  // "3 03 Mar March"  month
String.Format("{0:d dd ddd dddd}", dt);  // "9 09 Sun Sunday" day
String.Format("{0:h hh H HH}",     dt);  // "4 04 16 16"      hour 12/24
String.Format("{0:m mm}",          dt);  // "5 05"            minute
String.Format("{0:s ss}",          dt);  // "7 07"            second
String.Format("{0:f ff fff ffff}", dt);  // "1 12 123 1230"   sec.fraction
String.Format("{0:F FF FFF FFFF}", dt);  // "1 12 123 123"    without zeroes
String.Format("{0:t tt}",          dt);  // "P PM"            A.M. or P.M.
String.Format("{0:z zz zzz}",      dt);  // "-6 -06 -06:00"   time zone

回答by as-cii

Just use:

只需使用:

<TextBlock Text="{Binding Date, StringFormat=\{0:d\}}" />

回答by pawellipowczan

Be aware of the single quotes for the string format. This doesn't work:

请注意字符串格式的单引号。这不起作用:

    Content="{Binding PlannedDateTime, StringFormat={}{0:yy.MM.dd HH:mm}}"

while this does:

虽然这样做:

    Content="{Binding PlannedDateTime, StringFormat='{}{0:yy.MM.dd HH:mm}'}"

回答by Anurag

If you want add a string with the value use this:

如果要添加带有值的字符串,请使用以下命令:

<TextBlock Text="{Binding Date, StringFormat= 'Date : {0:d}'}" />