开发者

xaml date format string ignored

开发者 https://www.devze.com 2023-01-09 06:38 出处:网络
My label开发者_高级运维 is displaying \'7/27/2010\' instead of \'July 27, 2010\'. Can someone please tell me why my markup code is apparently ignored?

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,

Berryl


The 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}" />
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号