How do I localize the DatePicker?

In a small Wpf application, use a DatePicker binding to the DateTime property. When the user settings for the region and language, as well as the number and date format, are German, the date is displayed in German, and the calendar displays the names of the German months. Now I wanted to get it in American-English. In the c'tor MainWindow, I added before InitializeComponent () (same situation when executed after InitializeComponent ()):

string uiLanguage = ConfigurationManager.AppSettings["UILanguage"]; //"en-US"
Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(uiLanguage);
FrameworkElement.LanguageProperty.OverrideMetadata(typeof(FrameworkElement), new FrameworkPropertyMetadata(XmlLanguage.GetLanguage(uiLanguage)));


While this works with text fields, it does not affect the DatePicker. Then I was cheaky and created a new user "John English", registered as "John English", set my display language to English and the date and number format to US-English. The DatePicker now always displays the date in US English format, and the calendar shows the names of the English month, even when I set the language of my program to German. How can this be solved? Can it be allowed at all?


source to share

2 answers

In codebehind App.xaml add the following code:

public App()
    EventManager.RegisterClassHandler(typeof(DatePicker), DatePicker.LoadedEvent, new RoutedEventHandler(DatePicker_Loaded));

void DatePicker_Loaded(object sender, RoutedEventArgs e)
    var dp = sender as DatePicker;
    if (dp == null) return;

    var tb = GetChildOfType<DatePickerTextBox>(dp);
    if (tb == null) return;

    var wm = tb.Template.FindName("PART_Watermark", tb) as ContentControl;
    if (wm == null) return;

    wm.Content = "[Put your text here]";


[Ontopic;]] Try to set CurrentCulture and CurrentUICulture.

//Set default culture to Nl
System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");


ref for GetChildOfType



Create a normal DataGridTextColumn without binding in XAML:

<DataGridTextColumn x:Name="SomeDateDataGridColumn" Width="Auto" Header="Header"/> 


Then set the binding and string format in the code behind:

SomeDateDataGridColumn.Binding = new Binding("Property") { StringFormat = CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern };




All Articles