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?
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With