开发者

WPF Custom DateTimeFormat definitions in Culture

开发者 https://www.devze.com 2023-02-02 17:58 出处:网络
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 wel

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 th开发者_C百科e 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!


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?

0

精彩评论

暂无评论...
验证码 换一张
取 消