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开发者_StackOverflow中文版 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?
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
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
In the Converter use the below code
CultureInfo ci = new CultureInfo("en-GB");
return value.ToString(ci);
精彩评论