use*_*554 66 c# silverlight xaml
我有一个Silverlight 4应用程序.我有一个C#DateTime对象,我绑定到我的UI.我希望这个DateTime采用dd/mm/yyyy时间格式(TimeZone).例如,今天将显示为
04/07/2011 at 01:13 p.m. (EST)
Run Code Online (Sandbox Code Playgroud)
有办法做到这一点XAML吗?或者我需要构建转换器吗?
Vla*_*den 105
<TextBlock Text="{Binding Date, StringFormat='{}{0:MM/dd/yyyy a\\t h:mm tt}'}" />
Run Code Online (Sandbox Code Playgroud)
会回报你
04/07/2011于1:28 PM(-04)
Bro*_*ass 60
您可以在Silverlight 4中使用StringFormat来提供绑定到的值的自定义格式.
日期格式有很多选项.
对于"2004年4月17日下午1:52:45"的日期时间
您可以使用一组标准格式(标准格式)......
StringFormat=f : “Saturday, April 17, 2004 1:52 PM”
StringFormat=g : “4/17/2004 1:52 PM”
StringFormat=m : “April 17”
StringFormat=y : “April, 2004”
StringFormat=t : “1:52 PM”
StringFormat=u : “2004-04-17 13:52:45Z”
StringFormat=o : “2004-04-17T13:52:45.0000000”
Run Code Online (Sandbox Code Playgroud)
...或者您可以使用字母(自定义格式)创建自己的日期格式
StringFormat=’MM/dd/yy’ : “04/17/04”
StringFormat=’MMMM dd, yyyy g’ : “April 17, 2004 A.D.”
StringFormat=’hh:mm:ss.fff tt’ : “01:52:45.000 PM”
Run Code Online (Sandbox Code Playgroud)
ati*_*ker 10
你也可以使用
StringFormat=d
Run Code Online (Sandbox Code Playgroud)
在datagrid列中显示日期时间
最后它会
<sdk:DataGridTextColumn Binding="{Binding Path=DeliveryDate,StringFormat=d}" Header="Delivery date" Width="*" />
Run Code Online (Sandbox Code Playgroud)
输出看起来像

在SL5中我发现这个工作:
<TextBlock Name="textBlock" Text="{Binding JustificationDate, StringFormat=dd-MMMM-yy hh:mm}">
<TextBlock Name="textBlock" Text="{Binding JustificationDate, StringFormat='Justification Date: \{0:dd-MMMM-yy hh:mm\}'}">
Run Code Online (Sandbox Code Playgroud)
小智 6
C#:试试这个
使用一些分隔符,如下所示: