2010-07-27 85 views
2

我的标签显示'7/27/2010',而不是'2010年7月27日'。有人可以告诉我为什么我的标记代码显然被忽略吗?忽略xaml日期格式字符串

RibbonLabel Content="{Binding Source={x:Static sys:DateTime.Today}, StringFormat='{}{0:MMMM d, yyyy}'}" 

干杯,
Berryl

回答

11

如果被施加到String类型的属性的绑定的属性的StringFormat时才使用。由于内容是对象类型的,因此不使用它。取而代之的是内容的日期直接设置的,将其设置为一个TextBlock,使用的StringFormat设置TextBlock的Text属性:

<RibbonLabel> 
    <TextBlock Text="{Binding Source={x:Static sys:DateTime.Today}, 
     StringFormat='{}{0:MMMM d, yyyy}'}"/> 
</RibbonLabel> 

你也可以定义DateTime的一个DataTemplate,然后只将内容设置为今天:

<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}" /> 
+2

你是一个生命保护者。万分感谢** – Sayka 2015-04-08 21:43:09