0

WPF controls don't seem to have a .Cultureproperty, or any other obvious way of controlling how text is localised. Worse, on my test machine it doesn't even respect the system locale. My application needs to work for Germans, so the test machine runs a German version of windows and C# respects this. Thus

String.Format('{0}', 3.5) == "3,5"

But if I leave the localisation to WPF, e.g. by binding a numeric property directly to a label or a datagrid cell, I see some kind of American formatting. So if I format 3.5 as currency then I would see $3.50 on-screen.

So:

  • Why does WPF use the wrong locale?
  • How do I fix it?

Ideally "fix" means making it respect the system locale by default, while giving me explict control over particular things. Thus if I have a German invoice opened on an Australian PC, the ideal thing would be for a price to look like €3.50.

Adrian Ratnapala
  • 5,485
  • 2
  • 29
  • 39
  • Can you provide some code of your bindings? Normally it should use `CultureInfo.CurrentUICulture`. What does that output? – ZoolWay May 04 '14 at 11:34
  • @ZoolWay, apparently it doesn't (see Henk's answer). My `CurrentUICulture` and `CurrentCulture` are both German. Go figure. – Adrian Ratnapala May 04 '14 at 12:49

1 Answers1

3

WPF does indeed default to US, regardless of the System setings.

You can do an application-wide setting in App.Xaml.cs :

public partial class App : Application
{
    protected override void OnStartup(StartupEventArgs e)
    {   
        // Thread settings are separate and optional  
        // affect Parse and ToString:       
        // Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("nl-NL");
        // affect resource loading:
        // Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("nl-NL");

        FrameworkElement.LanguageProperty.OverrideMetadata(
              typeof(FrameworkElement),
              new FrameworkPropertyMetadata(
                    XmlLanguage.GetLanguage(
                          CultureInfo.CurrentCulture.IetfLanguageTag)));

        base.OnStartup(e);
    }
H H
  • 263,252
  • 30
  • 330
  • 514
  • And reading between the lines, I guess I can also set `.Language` on individual elements. Although it might be cleaner to convert decimal prices to strings explicitly in the ViewModel. – Adrian Ratnapala May 04 '14 at 12:01
  • Yes, use the ViewModel, especially when the formatting depends on the data (like your invoice). The UI is linked to the User. – H H May 04 '14 at 12:05