11

I have a Label:

<Label Name="lblBilledDate"
       Content="{Binding Path=BilledDate, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}">
</Label>

It is bound to a DateTime value.

How can i change the label to display the value to this: DisplayFormatString="dd MMM yyyy"

Currently the Label just dispalys: 1/1/2010

I need it to dispaly: 1 Jan 2010

Willem
  • 8,698
  • 16
  • 66
  • 90
  • I think the answer would involve adding some attributes to your binding expression, so this would be easier to answer if you hadn't removed the binding from the XAML you posted. – Joe White Mar 09 '11 at 17:55

2 Answers2

25

Use the ContentStringFormat attribute.

<Label x:Name="SomeLabel"
       Content="{Binding BilledDate}"
       ContentStringFormat="dd MMM yyyy" />

This is because Label inherits from ContentControl. Any ContentControl contains the ContentStringFormat attribute. Additionally, ItemsControl has ItemStringFormat and BindingBase has StringFormat.

John Cummings
  • 1,632
  • 3
  • 18
  • 34
user7116
  • 61,730
  • 16
  • 140
  • 170
2

What about this one?

<Label name="lblSomeLabel">
    <Binding Path="Date" StringFormat="{}{0:dd MMM yyyyy}"/>
</Label>
Vlad Bezden
  • 72,691
  • 22
  • 233
  • 168