To repro a bug I’ve created a simple WPF application that displays two labels bound to the same DateTime object. The bottom one is converted to a string using a IValueConverter. Both should be formatted the same but aren’t.
By adding some trace lines to the Convert method I’ve discovered that the CultureInfo (en-US) parameter is different from the CurrentThread.CurrentCulture (nl-NL). I’ve configured my English Windows 8.1 with Dutch formatting for datetime.
On stackoverflow someone pointed me to IFormattable and the ToString overload that would accept the culture. My Convert method now looks like below.
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture){ System.Diagnostics.Trace.TraceInformation( "Convert Culture: {0}", culture.ToString()); System.Diagnostics.Trace.TraceInformation( "Thread Culture: {0}", System.Threading.Thread.CurrentThread.CurrentCulture.ToString()); System.Diagnostics.Trace.TraceInformation( "ThreadUICulture: {0}", System.Threading.Thread.CurrentThread.CurrentUICulture.ToString()); var formattable = value as IFormattable; return formattable.ToString( null, culture); }
Now the formatting looks alike, but not what I expected. The formatting should have been in Dutch (dd-MM-yyyy).
By setting the Language property for the top framework element, the CultureInfo passed to the Convert method is changed. This has to be repeated on every Window. A very usefull tip from Rick shows how to do this in the OnStartup all at once.
protected override void OnStartup(StartupEventArgs e){ base.OnStartup(e); FrameworkElement.LanguageProperty.OverrideMetadata( typeof( FrameworkElement), new FrameworkPropertyMetadata(XmlLanguage .GetLanguage(CultureInfo.CurrentCulture.IetfLanguageTag))); }
Now the dates are printed the way you specify in the Control Panel > Region > Format. Remember to restart the Application (or Visual Studio when debugging) after you’ve changed the setting.
References
Stackoverflow answer to use IFormattable
WPF Bindings and CurrentCulture Formatting – Set Language App global