xaml日期格式字符串被忽略

Ber*_*ryl 2 wpf xaml

我的标签显示的是"2010年7月27日",而不是"2010年7月27日".有人可以告诉我为什么我的标记代码显然被忽略了吗?

RibbonLabel Content="{Binding Source={x:Static sys:DateTime.Today}, StringFormat='{}{0:MMMM d, yyyy}'}" 
Run Code Online (Sandbox Code Playgroud)

干杯,
Berryl

Qua*_*ter 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>
Run Code Online (Sandbox Code Playgroud)

您还可以为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}}">
Run Code Online (Sandbox Code Playgroud)

编辑:更简单的解决方案是使用ContentStringFormat属性

<RibbonLabel Content="{Binding Source={x:Static sys:DateTime.Today}}"
    ContentStringFormat="{}{0:MMMM d, yyyy}" />
Run Code Online (Sandbox Code Playgroud)

  • 你是救生员.万分感谢** (2认同)