改变 Silverlight 应用程序的文化
我目前正在开发 Silverlight 应用程序。我有一些数据网格/文本块,我使用标准绑定来显示值,其中一些是日期。例如,
<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}"/>
这绑定得很好,但日期始终显示为美国风格(月/日/年),而我想向它们显示英国风格(日/月/年)。我尝试使用托管应用程序的页面上
<param name="uiculture" value="en-GB" />
<param name="culture" value="en-GB" />
和 Silverlight 应用程序的 Application_Start
上的
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-GB");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-GB");
HTML 标记来设置区域性,但这些都没有任何区别。我有一个实现 IValueConverter 接口的自定义类,我在 Convert 方法上添加了断点,并且传入的 CultureInfo 参数是 en-US,如何更改区域性?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将以下行添加到我的表单的构造函数解决了此问题:
From http://timheuer.com/blog/archive/2010/08/11/stringformat-and-currentculture-in-silverlight.aspx
Adding the following line to the constructor of my form fixed this issue:
From http://timheuer.com/blog/archive/2010/08/11/stringformat-and-currentculture-in-silverlight.aspx
从代码中设置值违背了 MVVM 和依赖属性方法:您可能想查看这篇文章:
如何切换 UI 文化Silverlight 中的动态数据绑定
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