TextBlock 中的 WPF 格式日期时间?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1333052/
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
WPF format DateTime in TextBlock?
提问by Peter
I have a TextBlock
that is bound to a DateTime
property. How do I configure the format of the date?
我有一个TextBlock
绑定到一个DateTime
属性。如何配置日期的格式?
回答by Martin Harris
There is a string format property available when you are declaring the binding:
声明绑定时,有一个字符串格式属性可用:
<TextBox Text="{Binding Path=DateTimeValue, StringFormat=dd-MM-yyyy}" />
(You need to be on .NET 3.5 SP1 for this property to exist)
(您需要在 .NET 3.5 SP1 上才能存在此属性)
回答by Brian Hinchey
If you want to use a common format stringbetween bindings, you could declare the binding like this:
如果要在绑定之间使用通用格式字符串,可以像这样声明绑定:
<Textbox Text={Binding Path=DateTimeValue, StringFormat={x:Static local:Constants.DateTimeUiFormat}} />
With your constants class like this:
使用这样的常量类:
public static class Constants
{
public const string DateTimeUiFormat = "dd/MM/yyyy";
//etc...
}
回答by ZloyMakak
May be helpful to someone:
可能对某人有帮助:
<TextBlock Text="{Binding Source={x:Static sys:DateTime.Now},
StringFormat='{}{0: Today is dddd, MMMM dd, yyyy, hh:mm:ss}'}"/>
or 24h and 2digits month and year format:
或 24h 和 2digits 月份和年份格式:
<TextBlock Text="{Binding Source={x:Static sys:DateTime.Now},
StringFormat='{}{0: Today is dddd, MM.dd.yy, HH:mm:ss}'}"/>