Search code examples
wpfdatetimeformatculture

WPF Custom DateTimeFormat definitions in Culture


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!


Solution

  • 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?