×

First time here?

You are looking at the most recent posts. You may also want to check out older archives. Please leave a comment, ask a question and consider subscribing to the latest posts via RSS or email. Thank you for visiting!

I recently got a note about a nagging issue in using StringFormat in XAML binding expressions and how it doesn’t honor the current user’s culture settings.  This is true that there is an issue in that it doesn’t in WPF or Silverlight.  If you don’t know what I’m talking about, Silverlight introduced the ability to use StringFormat in data binding expressions (WPF has had this since 3.5 SP1) so you could do some formatting in-line in your binding.  Like this:

   1: <TextBlock Text="{Binding Path=CurrentDate, StringFormat=Current Timestamp is: \{0:G\}}" />

This would result in text that would be formatted directly using your string Formatter without the need for code-behind or any generic ValueConverter.  This is a very helpful feature for formatting UI values as well as in some cases replacing ValueConverters for simple tasks.

The problem is that StringFormat isn’t honoring the user’s culture settings.  Take for example this complete XAML:

   1: <StackPanel x:Name="FooContainer">
   2:  
   3:     <TextBlock x:Name="CultureInfo" />
   4:     <TextBlock x:Name="UICultureInfo" />
   5:  
   6:     <TextBlock Text="{Binding Path=CurrentDate, StringFormat=Current Timestamp is: \{0:G\}}" />
   7:  
   8:     <TextBlock x:Name="CostField" Text="{Binding Path=Cost, StringFormat=Cost is: \{0:c\}}" />
   9:  
  10:     <toolkit:GlobalCalendar  />
  11:  
  12: </StackPanel>

This is being bound to a simple object that exposes two properties for the purposes of demonstration: CurrentDate (DateTime) and Cost (double).  Using my standard US-English settings and regional preferences the output would be:

StringFormat with default culture

Now, let me tell my Silverlight app that I have a different culture information.  I can do this without having to force a language pack installation of sorts and completely change my machine.  Adding the culture/uiculture params to the <object> tag does the trick.  I’ll change it to “de-de” for German.  Here is the new output:

StringFormat with explicit culture

What?!  Even thought the settings recognize a different culture, StringFormat is not doing what I expect.  I would have expected a different date display for German settings (d.m.yyyy) and a different currency display instead of dollars.

Unfortunately this is an issue in StringFormat right now, but there is a simple workaround that if you are creating a localized app you can add to your code that shouldn’t affect your default language settings either.  In my constructor I add this line of code:

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

This tells the markup system to use the current culture settings as the UI language.  XmlLanguage is a part of the System.Windows.Markup namespace, so ensure you call that out explicitly or add a using statement.  Now refreshing my German settings sample I get:

StringFormat with explicit culture

as expected.  Changing (or removing the explicit setting of culture in my <object> tag) back to my default culture settings results in my US-English preferences being used and no need for me to change the XAML.

Hope this helps!


This work is licensed under a Creative Commons Attribution By license.


8/11/2010 11:34 AM | # re: StringFormat and CurrentCulture in Silverlight
Hi Tim,
I had a similiar problem recently. Your solution works for simple scenarios, but when introducing more complex controls things get a little more tricky. In particular I had issues with ChildWindow (mine where created in code, but I think XAML are the same) and the nested controls within not inheriting the Language from the parent.
Best, Mark
8/11/2010 4:26 PM | # re: StringFormat and CurrentCulture in Silverlight
Bryant -- in my sample (just one view) it was in mainpage.xaml -- but I'd say you want that prior to any UI being shown, so at any container user control level. Nesting controls (as Mark above notes) present some challenges to remember to put it in those places.
8/12/2010 3:13 AM | # re: StringFormat and CurrentCulture in Silverlight
Thanks for the heads up Tim... I'm not sure how MS could have missed something so obvious though!
8/18/2010 10:04 AM | # re: StringFormat and CurrentCulture in Silverlight
For WPF, the standard approach is to add the following in the Startup event of the application:

var culture = CultureInfo.CurrentCulture;
var lang = XmlLanguage.GetLanguage(culture.IetfLanguageTag);
FrameworkElement.LanguageProperty.OverrideMetadata(typeof(FrameworkElement), new FrameworkPropertyMetadata(lang));
Gravatar
11/25/2010 4:29 AM | # re: StringFormat and CurrentCulture in Silverlight
Nice work around!
2/8/2011 8:54 AM | # re: StringFormat and CurrentCulture in Silverlight
Oh man - sometimes the solution is just easy and the way to get there is like climbing the Mount Everst.
Thank you Tim for your blog - it saved my day (not to talk about the days of the last week)
6/9/2011 2:29 AM | # re: StringFormat and CurrentCulture in Silverlight
Thanks for post! It really helped, I was also struggling to figure why ut is not honoring locale for stringformat for dates
6/12/2011 2:27 AM | # re: StringFormat and CurrentCulture in Silverlight
Hi, Thank you so much for the blog. When you say: "Adding the culture/uiculture params to the <object> tag does the trick." What do you mean by <object> tag? may you elaborate on that please?
9/2/2011 11:09 AM | # re: StringFormat and CurrentCulture in Silverlight
THANKS Tim you saved my life, now that I´m learning the Silverlight world, and I haven´t got experience this issues like this.

Thank you very much.

@Houman: "<object> tag" means that in your web project, is the ASPX page that contains the reference to your Silverlight app. This reference is inside an normal HTML <object> tag. Well, the trick is that you can use the tags like

<param name="uiculture" value="es-ES"/>
<param name="culture" value="es-ES"/>

to specify the culture right there, instead of doing it programatically (another option).

I hope it helps.
Gravatar
1/24/2012 3:57 AM | # re: StringFormat and CurrentCulture in Silverlight
Hi,

I want to display the numeric values in linear axis of a chart. I have a configuration screen where user selects the option about the display format of y-axis values. These options are as under

0, 0.00, 0%, 0.00%, 0€, 0.00€

Below is the xaml of the dependent axis style where i can set the display format of y-axis.

==================================================================

<toolkit:LinearAxis x:Name="depAxis" Orientation="Y" ShowGridLines="True" ExtendRangeToOrigin="True">
<toolkit:LinearAxis.AxisLabelStyle>
<Style TargetType="toolkit:AxisLabel">
<Setter x:Name="axisFormat" Property="StringFormat" Value="{}{0:C}"/>
</Style>
</toolkit:LinearAxis.AxisLabelStyle>
</toolkit:LinearAxis>

==================================================================

Q. How can I display the user configured string format using styles.
3/12/2012 6:07 AM | # re: StringFormat and CurrentCulture in Silverlight
Tim what do you think about ConverterCulture?
Isn't the best way to solve the problem?
4/12/2012 8:11 AM | # re: StringFormat and CurrentCulture in Silverlight
Its nice workaround, but for me its very tricky, since I have about 50 xaml files. Is there any option to set this up in single place? Cheers?
12/19/2012 11:40 AM | # re: StringFormat and CurrentCulture in Silverlight
For me this solution worked perfectly, I already had a base class for all the views.

You can add this class for common functionalities:

public class ViewBase : PhoneApplicationPage
{
public ViewBase()
{
this.Language = XmlLanguage.GetLanguage(Thread.CurrentThread.CurrentCulture.Name);
}
...
}

And then, the root element of all your xaml will be something like:
<local:ViewBase
xmlns:local="clr-namespace:WP.UI.View"
>
. . .
</local:ViewBase>

Thanks Tim
8/14/2013 2:48 AM | # re: StringFormat and CurrentCulture in Silverlight
Thread.CurrentThread.CurrentUICulture = new CultureInfo("fr-BE");
this.Language = XmlLanguage.GetLanguage(Thread.CurrentThread.CurrentCulture.Name);

Doesn't change Settings in IE9

I
9/19/2013 8:56 AM | # re: StringFormat and CurrentCulture in Silverlight
Using Silverlight 5, the following still does not work:

<TextBlock Text="{Binding Path=CurrentDate, StringFormat=Current Timestamp is: \{0:G\}}" />

but changing on the XAML to the following does work:

<TextBlock>
<Run Text="{Binding Path=CurrentDate, StringFormat=Current Timestamp is: \{0:G\}}" />
</TextBlock>
7/4/2014 8:05 AM | # Excellent!
Thanks man! You've saved me!

I use this close to giving up and formatting in the ViewModel.

 
Please add 4 and 4 and type the answer here:

DISCLAIMER:

The opinions/content expressed on this blog are provided "ASIS" with no warranties and are my own personal opinions/content (unless otherwise noted) and do not represent my employer's view in any way.