ViktorZ
ViktorZ

Reputation: 951

Bug or Feature in TextBlock in Silverlight 4?

I have been having some fun with TextBlock bindings in Silverlight 4.
I have the following situation:

<TextBlock Text="{Binding Date, StringFormat=g}" />
<TextBlock>
    <Run Text="{Binding Date, StringFormat=g}"/>
<TextBlock>

Where Date is a property of type System.DateTime.
I haven't changed the current culture of the application.

The culture of my machine is Bulgaria (bg-BG). For example:

TextBlock 1: 11/16/2011 12:49 PM
TextBlock 2: 16.11.2011 г. 12:49 ч.

The interesting thing is that the first TextBlock formats the date and time using en-Us culture (or the default invariant one) while the second one uses bg-BG culture. Thing get even stranger since MSDN documentation for Silverlight TextBlock control says:

If the InlineCollection is created from XAML as inner text of a TextBlock object element, or if it is created by setting the Text property, the InlineCollection contains a single Run that contains that text.

Am I missing something here or it can be considered bug in Silverlight 4?

Upvotes: 5

Views: 556

Answers (1)

ChrisF
ChrisF

Reputation: 137128

We have found that you need to make sure that the Language of the control is set correctly to get it to honour the language settings correctly. In our case it was for currency symbols, but I assume the same problem occurs for dates as well.

Tim Heuer has a blog post entitled "StringFormat and CurrentCulture in Silverlight" about this.

The solution is to add the following line to the view constructor:

this.Language = XmlLanguage.GetLanguage(Thread.CurrentThread.CurrentCulture.Name);

This doesn't explain why it works correctly for the <Run>

Upvotes: 2

Related Questions