3
votes

I have a date column in a datagrid in my silverlight App. I need the time to be displayed in 24 hr format. To achieve this I have modified the UI culture in Application_Startup event like this:

    CultureInfo cinf = Thread.CurrentThread.CurrentUICulture.Clone() as CultureInfo;
    DateTimeFormatInfo f = cinf.DateTimeFormat.Clone() as DateTimeFormatInfo;
    System.Globalization.DateTimeFormatInfo df = new System.Globalization.DateTimeFormatInfo();
    string format = "MM/dd/yyyy HH:mm:ss";
    f.FullDateTimePattern = format;
    cinf.DateTimeFormat = f;
    Thread.CurrentThread.CurrentUICulture = cinf;

But the UI always picks up the datatime format specified in my regional settings on my OS. Any pointers?

2

2 Answers

0
votes

Would it not be better to define ther required formatting on the binding itself? Something like:-

<sdk:DataGridTextColumn Header="SomeDate" Binding="{Binding SomeDate, StringFormat={}{0:MM/dd/yyyy HH:mm:ss}}" />

The reason why your code doesn't work is that the CultureInfo used by controls loaded by Xaml is defined by the Language property of control loading the Xaml. I don't think there is a way to provide a custom CultureInfo in this scenario.

0
votes

Try setting CurrentCulture as well, that is:

Thread.CurrentThread.CurrentCulture = cinf;