spacer
  • Home
  • Contact
  • Archives
  • Follow
  • Subscribe
×

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!
  Smashing Magazine August 2010 Windows 7 Theme | | Pano versus Pivot: Windows Phone Design Days content available  

StringFormat and CurrentCulture in Silverlight

posted @ Wednesday, August 11, 2010 9:44 AM | 13 Comments

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:

spacer

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:

spacer

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:

spacer

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!

tags: silverlight, xaml, wpf, localization, ria, stringformat, currentculture

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

  Smashing Magazine August 2010 Windows 7 Theme | | Pano versus Pivot: Windows Phone Design Days content available  

spacer
Mark Cooper
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
spacer
timheuer
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.
spacer
JasonBSteele
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!
spacer
Richard
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));
spacer
Sam
11/25/2010 4:29 AM | # re: StringFormat and CurrentCulture in Silverlight
Nice work around!
spacer
achim.w
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)
spacer
dove
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
spacer
Houman
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?
spacer
imarrero
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.
spacer
mak
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.
spacer
Michele Adamo
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?
spacer
kostkac
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?
spacer
Niurka
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


Please add 4 and 7 and type the answer here:
spacer
via Twitter:
Follow @timheuer

Ads by Lake Quincy Media

  • Best Bets
  • Callisto - a WinRT XAML Toolkit
  • Introducing Callisto: A WinRT XAML toolkit
  • Using SQLite in a Metro style App
  • Building custom controls for Metro style XAML
  • A guide to Silverlight 5
  • Getting Started with Silverlight Tutorials
  • posts about WinRT XAML
  • posts about Silverlight
  • info on FREE Foxit PDF Previewer

  • Recent Posts
  • Implement a ‘smart banner’ for your Windows Store app
  • Remote Debugging your Windows Store app on your Surface
  • Using Azure Web Sites to market your Windows App
  • Using vector data for AppBar icons in XAML
  • New-DiffGist: a powershell cmdlet for posting diffs fast
  • UPDATED HOWTO: SQLite with Windows 8 apps
  • Seeding your Metro style app with a SQLite database

  • Recent Comments
  • (Imran) Dazzling post!!!I am very happy to read Microsoft ...
  • (Joe M4dM4n Connor) Database migrating is a really important task, but...
  • (kumar) You actually make it appear so easy with your perf...
  • (Usha Rajagopal ) Thank you and looking for more posts. I am really ...
  • (.) ONLY sell on ebay if you are accepting cash on col...
  • (albert)
gipoco.com is neither affiliated with the authors of this page nor responsible for its contents. This is a safe-cache copy of the original web site.