Friday, March 1, 2013

Fixing string formatting locale for Windows Phone

This has caused me so much frustration!  My Rebate Tracker and Expense Tracker apps only support decimal points in currency (disallowing commas), and only format the date in MM/DD/YYYY style.  I tried switching the regional settings on the emulator and there was simply no change, yet I was using the standard StringFormat in the databinding, or string.Format behind the scenes.

Apparently this is a known bug, but I didn't come across a solution until I finally stumbled across an old post by Tim Heuer (always worth reading).  It turns out, that the very simple solution is to set the Language property of the page.  I had never noticed this property before, and I wouldn't have expected it to be required!

All that you need to do is set the property in the constructor of the page.  To make it even easier, you can create a base page object (if you don't have one already) and set the property there.

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

This simple line makes such a difference in creating international apps!  Definitely something to add to my list of tricks.

Source: http://timheuer.com/blog/archive/2010/08/11/stringformat-and-currentculture-in-silverlight.aspx