问题描述
在WPF中我试图绑定一个日期属性Datagrid的,如果我只有绑定的DateTime格式的日期属性,我可以在DataGrid排序。
In WPF I am trying to Bind a Date property to Datagrid, and If I only have the date Property binded as DateTime format I can sort it in the datagrid.
在这里,我必须展示区域的日期,如果用户更改区域和语言设置,应用程序应该显示resepective时间格式。
And Here I have to show the regional Date, If the user changes the Region and Language settings, The application should show the resepective time format.
下面谈到的问题,当日期为字符串格式,它根据区域设置的鞋子,但是当属性DateTime格式是绑定的日期dafaulted美国的格式,而不是当期的区域设置显示。
Here comes the Issue, When the Date is in string Format , it shoes according to the region settings, but when the property is binded in DateTime format the date is dafaulted to American Format rather than showing with currect region settings.
所以,如果我有字符串格式排序按列不工作,如果它在DateTime格式,它并转换为区域设置
So, If I have string format the sorting by column doesnt work, and if its in DateTime format, it does convert to regional settings
推荐答案
这是因为绑定系统采用由<$c$c>FrameworkElement.Language$c$c>属性,它不会自动匹配当前的文化(这是一个有点傻IMO,但是这事情是这样的......)。
That's because the binding system uses the culture defined by the FrameworkElement.Language
property, which doesn't automatically match the current culture (which is a bit silly IMO, but that's the way it is...).
幸运的是它周围的一个简单的方法,你只需要覆盖在应用程序中的静态构造函数中的语言
属性中的元数据的的:
Fortunately there's an easy way around it, you just need to override the metadata for the Language
property in your application static constructor, as shown here:
public partial class App : Application
{
static App()
{
FrameworkElement.LanguageProperty.OverrideMetadata(
typeof(FrameworkElement),
new FrameworkPropertyMetadata(
XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.IetfLanguageTag)));
}
}
这篇关于日期时间不能与数据网格的CurrentCulture格式呈现,ListView控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!