我有一个 Silverlight 4 应用程序。我有一个 C# 的 DateTime 对象,我要将其绑定到 UI 上。我希望这个 DateTime 格式为 dd/mm/yyyy
时间(时区)。例如,今天将显示为:
04/07/2011 at 01:13 p.m. (EST)
有没有办法通过XAML
来实现这个?或者我需要构建一个转换器?
我有一个 Silverlight 4 应用程序。我有一个 C# 的 DateTime 对象,我要将其绑定到 UI 上。我希望这个 DateTime 格式为 dd/mm/yyyy
时间(时区)。例如,今天将显示为:
04/07/2011 at 01:13 p.m. (EST)
有没有办法通过XAML
来实现这个?或者我需要构建一个转换器?
<TextBlock Text="{Binding Date, StringFormat='{}{0:MM/dd/yyyy a\\t h:mm tt}'}" />
将返回您
04/07/2011 下午1:28(-04)
您可以在Silverlight 4中使用StringFormat来自定义绑定值的格式。
日期格式化有大量选项。
对于“2004年4月17日下午1:52:45”的DateTime,
您可以使用一组标准格式(标准格式)...
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”
...或者您可以使用字母创建自己的日期格式(自定义格式)
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”
StringFormat=d
在您的数据网格列中显示日期时间
最终它会是这样的
<sdk:DataGridTextColumn Binding="{Binding Path=DeliveryDate,StringFormat=d}" Header="Delivery date" Width="*" />
输出将类似于
<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\}'}">
C#: 试试这个
使用一些分隔符,像这样:
对我来说,这个方法有效:
<TextBlock Text="{Binding Date , StringFormat=g}" Width="130"/>
<TextBlock Text="{Binding Date , StringFormat=G}" Width="130"/>
<TextBlock Text="{Binding Date , StringFormat=G}" Width="130" Language="fa-IR"/>
Label
而不是TextBlock
,请记住需要使用ContentStringFormat
而不是设置 Binding StringFormat。 - Slate