Language and CurrentCulture in Silverlight and WPF

In WinForms and ASP.NET, numbers and dates are formatted and parsed using the current thread's CultureInfo.CurrentCulture property. This is the value that users may change using the control panel. In Silverlight and WPF, numbers and dates are formatted and parsed using the containing element’s Language property, which always defaults to English (that's baked into the platform at the Binding level). Using mismatched values for CurrentCulture and Language is a bad idea, and the problem is aggravated by the fact that even Microsoft screwed this up on their toolkit controls (some use CurrentCulture instead of Language as they should). The best solution is to add this code to all your localizable applications:

using System.Windows.Markup;  

// get the CurrentCulture (this is set by users in the Control Panel)  
var ct = System.Threading.Thread.CurrentThread;  

// set the page language to match the CurrentCulture  
var langName = ct.CurrentCulture.TwoLetterISOLanguageName;  
this.Language =XmlLanguage.GetLanguage(langName);  

Once you do this, both CurrentCulture and Language will match, which means the Binding and CurrentCulture information will work correctly. The sample application attached that shows how this works. The sample assigns Portuguese to the CurrentCulture and Language, which allows you to type dates in the d/M/yyyy format. For example, typing "31/1/10" sets the date to January 31, 2010. Download Sample


GrapeCity Developer Tools
comments powered by Disqus