我的标签显示'2010年7月27日'而不是'2010年7月27日'。有人可以告诉我为什么我的标记代码显然被忽略了吗?
RibbonLabel Content="{Binding Source={x:Static sys:DateTime.Today}, StringFormat='{}{0:MMMM d, yyyy}'}"
干杯,
Berryl
答案 0 :(得分:11)
仅当绑定应用于String类型的属性时,才使用StringFormat属性。由于Content是object类型,因此不使用它。不是直接将内容设置为日期,而是将其设置为TextBlock,并使用StringFormat设置TextBlock的Text属性:
<RibbonLabel>
<TextBlock Text="{Binding Source={x:Static sys:DateTime.Today},
StringFormat='{}{0:MMMM d, yyyy}'}"/>
</RibbonLabel>
您还可以为DateTime定义DataTemplate,然后将内容设置为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}}">
编辑:更简单的解决方案是使用ContentStringFormat属性
<RibbonLabel Content="{Binding Source={x:Static sys:DateTime.Today}}"
ContentStringFormat="{}{0:MMMM d, yyyy}" />