Silverlight用户界面文化
我在silverlight应用程序的数据网格中有一个日期列。我需要以24小时格式显示时间。为了实现这一点,我修改了应用程序启动事件中的UI区域性,如下所示:Silverlight用户界面文化,silverlight,datagrid,silverlight-4.0,Silverlight,Datagrid,Silverlight 4.0,我在silverlight应用程序的数据网格中有一个日期列。我需要以24小时格式显示时间。为了实现这一点,我修改了应用程序启动事件中的UI区域性,如下所示: CultureInfo cinf = Thread.CurrentThread.CurrentUICulture.Clone() as CultureInfo; DateTimeFormatInfo f = cinf.DateTimeFormat.Clone() as DateTimeFormatInfo; Syst
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;
但是UI总是选择在我的操作系统上的区域设置中指定的数据时间格式。
有指针吗?也可以尝试设置
CurrentCulture
,即:
Thread.CurrentThread.CurrentCulture = cinf;
在绑定本身上定义所需的格式不是更好吗?比如:-
<sdk:DataGridTextColumn Header="SomeDate" Binding="{Binding SomeDate, StringFormat={}{0:MM/dd/yyyy HH:mm:ss}}" />
代码不起作用的原因是Xaml加载的控件使用的
CultureInfo
是由加载Xaml的控件的Language
属性定义的。我认为在这种情况下没有办法提供自定义的CultureInfo
。是的,我尝试过设置CurrentCulture和CurrentUICulture,但UI仍然以12小时格式呈现。有趣的是,当我在视图模型类上放置断点时,格式显示为24小时格式!!!我绝对需要返回DateTime对象,我不能使用返回字符串的绑定转换器。我在每个列上都有过滤器,这些过滤器取决于列的数据类型。我在XAML中尝试了“Language”属性,文化基于我设置的语言反映出来,但即使是该语言的设置也会从操作系统设置中获取???。是否有其他方法可以覆盖此属性???@avinash:通常情况下,人们会通过ICollectionView
的实现进行过滤,这将在发生任何绑定之前应用于传入的对象集。除此之外,我不认为StringFormat
适用,除非分配给的目标属性要求使用String
(对于DataGridTextColumn
,这是正确的)。也许a)您应该试一试,b)如果没有,请包含更多关于xaml和代码的细节。