Question

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.

Was it helpful?

Solution

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);
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top