0

I need to set the culture to german while keeping the US number format. Therefore, I'm setting

var culture = new System.Globalization.CultureInfo("de-DE");
var usCulture = new System.Globalization.CultureInfo("en-US");
culture.NumberFormat = usCulture.NumberFormat;
CultureManager.UICulture = culture;

The CultureManager is from Infralution and enables the switching of languages on the fly. I can verify that the NumberFormat is indeed set to US format with e.g. a "." as decimal seperator after above code. Nevertheless, the following expression

<TextBox Text="{Binding MyDoubleVariable, StringFormat={}{0:0.#}}"/>

where MyDoubleVariable = 2.13 is displayed as 2,13. Where's my mistake? How can I retain the US format?

peter
  • 2,103
  • 7
  • 25
  • 51
  • _I can verify that the NumberFormat is indeed set to US format_ how? – 1.618 Apr 16 '15 at 20:06
  • Have a look at http://stackoverflow.com/questions/2764615/wpf-stringformat-0c-showing-as-dollars. I am not flagging as duplicate because I have gold badge in C# leading to instant closing of your question if I do and I am not entirely sure the linked question will solve your problem. – Martin Liversage Apr 16 '15 at 20:06
  • Breakpoint before and after => Culture is changed from US to DE and number format remains US. – peter Apr 16 '15 at 20:07
  • Martin, thanks for not instantly closing my question ;o) The problem in your linked thread is not what I am facing. I'm on a german pc and can successfully start with englisch number settings but german language. My problem occurs when switching the language to english and then back to german. – peter Apr 16 '15 at 20:12
  • @peter: The answers to the linked questions explain how you can modify the culture that is used formatting in data binding. You are certainly not doing the right thing because setting `CultureManage.UICulture` does not affect formatting in data binding. – Martin Liversage Apr 16 '15 at 20:29
  • @Martin, I'm definitly not doing the right thing. However, what I want is to keep the culture that is used for formatting numbers in data binding fixed while using the CultureManager to exchange {Resx .. }-language bindings. It's working as supposed with a converter. – peter Apr 16 '15 at 21:00

2 Answers2

2

WPF will use the system culture when performing the formatting. Not aware of the framework to which you refer.

Try creating a custom IValueConverter which takes the culture as a parameter.

To your converter you can create a string property for your format string.

Then in your xaml you can create a convertor instance for each format string you want to use - for example you could have one for money and another for numbers. Add the convertor to your binding and pass a culture specifier as a parameter at that time.

Sorry no code, answering on mobile.

kidshaw
  • 3,423
  • 2
  • 16
  • 28
0

As said by @kidshaw WPF uses the system language by default, but you can override it.

Setting Culture (en-IN) Globally in WPF App

How to set and change the culture in WPF

So you should probably do this in public static App() { ... } (at least that's where we do it usually).

Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
FrameworkElement.LanguageProperty.OverrideMetadata(
    typeof(FrameworkElement),
    new FrameworkPropertyMetadata(
        XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.IetfLanguageTag)));

Then change the language to German where you would usually do it. This should keep the formatted stuff in XAML using the US format, but everything else in German.

Community
  • 1
  • 1
Staeff
  • 4,994
  • 6
  • 34
  • 58