Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xaml date format string ignored

Tags:

wpf

xaml

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

like image 957
Berryl Avatar asked May 02 '26 15:05

Berryl


1 Answers

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}" />
like image 98
Quartermeister Avatar answered May 04 '26 04:05

Quartermeister