paul
paul

Reputation: 13516

Why is binding result different on a Label and a TextBox?

I was using the following XAML:

<Label Grid.Row="0" Grid.Column="0" Content="Datum"/>
<Label Grid.Row="0" Grid.Column="1" Content="{Binding TimeStamp, StringFormat={}{0:yyyy-MM-dd HH:mm:ss.fff}}"/>
<Label Grid.Row="0" Grid.Column="2" Content="Level"/>
<Label Grid.Row="0" Grid.Column="3" Content="{Binding Level}"/>

but the TimeStamp was being formatted like this:

2.24.2012 7:38

I started up Snoop (great tool!) and noticed that the Label is actually composed of a TextBox and that this TextBox contained the TimeStamp formatted as I defined it. I then replaced the Label with a TextBox and I get the TimeStamp correctly formatted.

<TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding TimeStamp, StringFormat={}{0:yyyy-MM-dd HH:mm:ss.fff}}"/>

2012-02-24 07:38:23.123

I have defined no Resource, Trigger or Style blocks to override Label behaviour so I'm wondering why this is happening.

Any ideas?

Upvotes: 3

Views: 422

Answers (1)

Terry Ma
Terry Ma

Reputation: 369

The Binding.StringFormat property doesn't work on Labels, you need to use the ContentStringFormat property on the Label

<Label Grid.Row="0" Grid.Column="1" Content="{Binding TimeStamp}">
    <Label.ContentStringFormat>0:yyyy-MM-dd HH:mm:ss.fff</Label.ContentStringFormat>
</Label>

also see Binding only part of a label

Upvotes: 4

Related Questions