3

In a small Wpf application, use a DatePicker bound to a DateTime property. When the user's region and language settings, and number and date format are German, the date is displayed in German, and the calendar shows German month names. Now I wanted to get it in US-English. In the c'tor of MainWindow I added before InitializeComponent() (same situation when doing that 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 that works with textboxes, it has no effect with the DatePicker. Then I was cheaky and created a new user "John English", logged in as "John English", set his display language to English and the date and number format to US-English. Now the DatePicker always displays the date in the US-English format and the calendar shows English month names, even when I set the language of my program to German. How can that be resolved? Can it be resolved at all?

Bernhard Hiller
  • 2,163
  • 2
  • 18
  • 33

2 Answers2

7

In the codebehind of the 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 setting both 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

Community
  • 1
  • 1
Ralf de Kleine
  • 11,464
  • 5
  • 45
  • 87
  • Thanks for the hint. But that's not the point: I am not at all interested in replacing "Select Date" by my own localized text. The date format is different between en-US and de-DE. When I set my application's language to en-US on a German Windows, I want to see the date formatted in American style. – Bernhard Hiller Mar 28 '12 at 13:26
  • Thanks. Setting the CurrentCulture, not only CurrentUICulture, did the trick. – Bernhard Hiller Mar 28 '12 at 14:15
  • **Error:** the name `"GetChildOfType"` does not exist in current context – Hisham Mar 02 '17 at 17:57
  • http://stackoverflow.com/questions/10279092/how-to-get-children-of-a-wpf-container-by-type – Ralf de Kleine Mar 03 '17 at 10:09
  • Setting both `CurrentCulture` and `CurrentUICulture` works fine. I suggest to use as main solution, instead of _manually_ replace texts in the pickers. – Tognolo Aug 09 '22 at 06:51
0

Create a normal DataGridTextColumn without any bindings in the XAML:

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

Then set the binding and stringformat in the code behind:

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