Click here to Skip to main content
15,878,748 members
Articles / Programming Languages / C#
Tip/Trick

Regional date formats and Silverlight 4

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
2 Oct 2011CPOL1 min read 16.4K   3   3
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:


C#
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)


Written By
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

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

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

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