5

I am working on a Silverlight app at the moment. I have a few datagrids/textblocks where I use standard binding to show values, some of which are dates. e.g.

<sdk:DataGrid AutoGenerateColumns="False" IsReadOnly="True" ItemsSource="{Binding Path=MyCollection}">
  <sdk:DataGrid.Columns>
    <sdk:DataGridTextColumn Binding="{Binding Path=Name, Mode=OneWay}" Header="Agent"/>
    <sdk:DataGridTextColumn Binding="{Binding Path=UpdateTime, Mode=OneWay}" Header="Update Time"/>
  </sdk:DataGrid.Columns>
</sdk:DataGrid>
<TextBlock Text="{Binding Path=LastUpdatedTime}"/>

This binds fine but the dates are always shown as US style (m/d/y) whereas I want to show them UK style (d/m/y). I have tried setting the culture using both the HTML tags on the page hosting the application

<param name="uiculture" value="en-GB" />
<param name="culture" value="en-GB" />

and on Application_Start of my Silverlight application

Thread.CurrentThread.CurrentCulture = new CultureInfo("en-GB");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-GB");

but neither of these make any difference. I have a custom class that implements IValueConverter interface, I added a breakpoint on the Convert method and the CultureInfo parameter that is passed in is en-US, how do I change the culture?

Fermin
  • 34,961
  • 21
  • 83
  • 129

3 Answers3

4

Adding the following line to the constructor of my form fixed this issue:

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

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

Fermin
  • 34,961
  • 21
  • 83
  • 129
0

Setting the value from code kind of goes against the MVVM and dependency property approach: you might want to see this SO article: How to switch UI Culture of data binding on the fly in Silverlight

Community
  • 1
  • 1
Quango
  • 12,338
  • 6
  • 48
  • 83
-1

In the Converter use the below code

CultureInfo ci = new CultureInfo("en-GB");
return value.ToString(ci);
Ragunathan
  • 2,007
  • 1
  • 14
  • 10
  • Would I need to use a converter for each DataColumn/TextBlock that displays a date? – Fermin Jul 01 '10 at 11:56
  • yes. But if you want to use global, In the MainPage constructor or Loaded event use below (No need for converter here) CultureInfo en = new CultureInfo("en-GB"); Thread.CurrentThread.CurrentCulture = en; – Ragunathan Jul 01 '10 at 12:00
  • As I said in my initial post i've already tried setting the CurrentCulture in the Application_Start event and it doesn't change how the dates are shown. – Fermin Jul 01 '10 at 12:52