文化中的 WPF 自定义 DateTimeFormat 定义
我的 WPF 应用程序中的 DateTime 字符串格式一直存在问题。我实现了我在这里找到的国际化修复:http://www .nbdtech.com/Free/WpfBinding.pdf 以及几个不同的 StackOverflow 问题。
这对于文化/区域特定的日期时间字符串格式非常有用。但是,当我对区域性中的日期时间格式进行自定义更改时,如下所示:
CultureInfo ci = new CultureInfo("en-US");
ci.DateTimeFormat.DateSeparator = ".";
在某些控件中,它完全被忽略,例如:DataGrid。而在其他诸如 RichTextBox 和 DatePicker 中它工作得很好。这是我在 App.xaml.cs 中的 OnStartup 方法:
protected override void OnStartup(StartupEventArgs e)
{
// Set CultureInfo
CultureInfo ci = new CultureInfo("en-US");
ci.DateTimeFormat.DateSeparator = ".";
Thread.CurrentThread.CurrentCulture = ci;
Thread.CurrentThread.CurrentUICulture = ci;
FrameworkElement.LanguageProperty.OverrideMetadata(
typeof(FrameworkElement),
new FrameworkPropertyMetadata(
XmlLanguage.GetLanguage(
CultureInfo.CurrentCulture.IetfLanguageTag)));
base.OnStartup(e);
}
有什么想法吗?
谢谢!
I've been having problems with the stringformat of DateTime in my WPF application. I implemented the The Internationalization Fix which I found here: http://www.nbdtech.com/Free/WpfBinding.pdf as well as in several different StackOverflow questions.
This works great for culture/locale specific DateTime stringformats. However, when I do custom changes to the datetimeformat in the culture like so:
CultureInfo ci = new CultureInfo("en-US");
ci.DateTimeFormat.DateSeparator = ".";
It's completely ignored in some controls, for example: DataGrid. While in others such as RichTextBox and DatePicker it works fine. Here's my OnStartup method in App.xaml.cs:
protected override void OnStartup(StartupEventArgs e)
{
// Set CultureInfo
CultureInfo ci = new CultureInfo("en-US");
ci.DateTimeFormat.DateSeparator = ".";
Thread.CurrentThread.CurrentCulture = ci;
Thread.CurrentThread.CurrentUICulture = ci;
FrameworkElement.LanguageProperty.OverrideMetadata(
typeof(FrameworkElement),
new FrameworkPropertyMetadata(
XmlLanguage.GetLanguage(
CultureInfo.CurrentCulture.IetfLanguageTag)));
base.OnStartup(e);
}
Any ideas?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
哎呀,这看起来是一个非常令人讨厌的解决方法。我担心它可能产生其他副作用,特别是在处理日期和时间控制时。为什么不使用值转换器呢?
Yikes that looks like a pretty nasty workaround. I'd be afraid of other side-effects it might have, particularly when dealing with date and time controls. Why not use a value converter instead?