My label开发者_高级运维 is displaying '7/27/2010' instead of 'July 27, 2010'. Can someone please tell me why my markup code is apparently ignored?
RibbonLabel Content="{Binding Source={x:Static sys:DateTime.Today}, StringFormat='{}{0:MMMM d, yyyy}'}"
Cheers,
BerrylThe StringFormat property is only used if the binding is applied to a property of type String. Since Content is of type object, it is not used. Instead of setting the content to the date directly, set it to a TextBlock, and set the Text property of the TextBlock using a StringFormat:
<RibbonLabel>
<TextBlock Text="{Binding Source={x:Static sys:DateTime.Today},
StringFormat='{}{0:MMMM d, yyyy}'}"/>
</RibbonLabel>
You could also define a DataTemplate for DateTime and then just set the content to Today:
<Window.Resources>
<DataTemplate DataType="{x:Type sys:DateTime}">
<TextBlock Text="{Binding StringFormat='{}{0:MMMM d, yyyy}'}"/>
</DataTemplate>
</Window.Resources>
...
<RibbonLabel Content="{Binding Source={x:Static sys:DateTime.Today}}">
EDIT: An even simpler solution is to use the ContentStringFormat property
<RibbonLabel Content="{Binding Source={x:Static sys:DateTime.Today}}"
ContentStringFormat="{}{0:MMMM d, yyyy}" />
精彩评论