Click here to Skip to main content
Click here to Skip to main content
Go to top

Regional date formats and Silverlight 4

, 2 Oct 2011
Rate this:
Please Sign up or sign in to vote.
How to get Silverlight 4 to display dates by default using the regional settings in the Control Panel.

I am writing a Silverlight application which will be used in multiple countries by a large organisation. The users are all used to working in English, so there's no requirement for a complex internationalization approach.

However, the one thing which causes major annoyance is for dates to appear in the American mm/dd/yyyy format in the rest of the world.

So I was rather upset when I realised that although my PC is set to the UK regional settings, my Silverlight app ignored this and insisted on formatting DateTime fields as mm/dd/yyyy when, for example, I displayed a date in a DataGrid column.

My initial attempts to fix this found me loads of articles about complex internationalization techniques in Silverlight and .NET, and it started to look as though I'd have no choice but to write custom-handlers for all my date fields.

Fortunately, I then discovered some references to the System.Windows.Markup.XmlLanguage class. All I needed to do was add a few lines to my MainPage() routine as follows:

using System.Windows.Markup;
using System.Threading;
.....
    public MainPage()
    {       
     this.Language = XmlLanguage.GetLanguage(Thread.CurrentThread.CurrentCulture.Name);
     // Now the rest of the constructor as it was before
     InitializeComponent();
       ......

and suddenly my dates looked as I wanted them to.

Depending on the way you structure your code, you may need to add a similar line in the constructors of other UI elements, but so far I've found that just this one line does the trick.

To be honest, I don't really understand why this behaviour isn't the default. Silverlight knows perfectly well what language you want - the main thread has the information - but it doesn't propagate that up to the UI elements unless you do it explicitly.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Gary Bilkus

United Kingdom United Kingdom
No Biography provided

Comments and Discussions

 
GeneralSilverlight 5 appears to have the same problem but your fix worked great =) PinmemberPaul Cooper1-Mar-13 4:18 
GeneralRichard, I think you are quite right that my approach uses t... PinmemberGary Bilkus5-Oct-11 10:01 
GeneralDoes that use the user's settings, or the default settings f... PinmemberRichard Deeming5-Oct-11 9:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140916.1 | Last Updated 2 Oct 2011
Article Copyright 2011 by Gary Bilkus
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid