-1

I am trying to change the number formatting in my WPF application. I want international standardized formatting (space as thousand separator), and also "--" as NaN symbol.

I clone a CultureInfo and set the NumberFormatInfo, then set that as Thread.CurrentThread.CurrentCulture in the constructor of the main window (I also tried in WindowLoaded). I also set CultureInfo.DefaultThreadCurrentCulture, and *CurrentUICulture for good measure.

But nothing works. The number formatting in Labels stays as default US format (comma as thousand separator). Why does the app not respond to my setting the culture?

Here is the full code, although I've tried many variations of it so I suspect the actual code is not the problem, but that I am misunderstanding something about how cultures work:

        CultureInfo myNumberCulture = (CultureInfo)CultureInfo.InvariantCulture.Clone();
        myNumberCulture.NumberFormat = new NumberFormatInfo
        {
            NaNSymbol = "--",
            NumberGroupSeparator = " ",
            PercentGroupSeparator = " ",
            CurrencyGroupSeparator = " ",
            NumberDecimalSeparator = ".",
            PercentDecimalSeparator = ".",
            CurrencyDecimalSeparator = "."
        };
        Thread.CurrentThread.CurrentCulture = myNumberCulture;
        CultureInfo.DefaultThreadCurrentCulture = myNumberCulture;
GrixM
  • 215
  • 2
  • 4
  • 20
  • 1
    Well how are you adding the text to the labels? – DavidG Jun 03 '19 at 12:59
  • @DavidG It varies, usually I'm setting Content. And it is set to numeric values, not strings, so the labels should format it themselves. – GrixM Jun 03 '19 at 13:41
  • Labels don't take numbers, they take strings so you must be converting them somehow. – DavidG Jun 03 '19 at 13:41
  • 1
    The simplest and most reliable approach is to apply a stringformat. Which of course isn't automatic. The other reliable alternative is to subclass binding https://stackoverflow.com/questions/5831455/use-real-cultureinfo-currentculture-in-wpf-binding-not-cultureinfo-from-ietfl – Andy Jun 03 '19 at 13:42
  • 1
    [This may be helpful](https://stackoverflow.com/questions/7454024/setting-culture-en-in-globally-in-wpf-app). `FrameworkElement.LanguageProperty.OverrideMetadata(...)` is the only part that affects the UI in the brief testing I've done, you have to refresh all bindings (`OnPropertyChanged("")`) to update the UI, it throws an exception if you do it twice, and for me it's only working properly with known cultures like `"de-DE"` etc. – 15ee8f99-57ff-4f92-890c-b56153 Jun 03 '19 at 13:44
  • 2
    @DavidG "Labels don't take numbers" is actually not true. The Content property of the WPF Label class is of type object and accepts any type of value, especially also numeric values. – Clemens Jun 03 '19 at 14:37

1 Answers1

0

The crux seems to be that Labels ignores the thread's culture when doing the built-in formatting of numbers. However ToString does not. So my solution/workaround was to explicitly call ToString to the number before setting it as the label's Content.

GrixM
  • 215
  • 2
  • 4
  • 20