1

I create a Textbox in Code behinde and bind it to a double Property.

                TextBox t = new TextBox();
                t.Width = 80;
                t.DataContext = s;         
                Binding binding = new Binding();
                binding.Mode = BindingMode.TwoWay;
                binding.Path = new PropertyPath("Value");
                BindingOperations.SetBinding(t, TextBox.TextProperty, binding);

When I enter a value like 45,45 (comma) it is parsed to 4545.

If I enter 45.45 (point) it is parsed correctly with 45,45.

I use German language Settings and my decimal separator is ,.

What am I doing wrong?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
TheJoeIaut
  • 1,522
  • 2
  • 26
  • 59

3 Answers3

3

Try setting binding.ConverterCulture to your target culture.

For example

 binding.ConverterCulture = CultureInfo.CurrentCulture;
Phil
  • 42,255
  • 9
  • 100
  • 100
  • I'm glad it works, but Dummy01's answer may be better. Give it a try. http://stackoverflow.com/questions/5831455/use-real-cultureinfo-currentculture-in-wpf-binding-not-cultureinfo-from-ietfl – Phil Mar 14 '12 at 13:14
2

A clear, not culture specific solution is to add this to your App.xaml.cs and normally WPF will always use the correct culture - regional settings:

    static App()
    {
        FrameworkElement.LanguageProperty.OverrideMetadata(
            typeof(FrameworkElement),
            new FrameworkPropertyMetadata(XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.IetfLanguageTag)));
    }
Dummy01
  • 1,985
  • 1
  • 16
  • 21
  • Strange... if you do a breakpoint there what value the XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.IetfLanguageTag) returns? – Dummy01 Mar 14 '12 at 13:30
  • actually it isn't called at all – TheJoeIaut Mar 14 '12 at 13:33
  • This is impossible because this is the static constructor of your application. It should be called once during the startup. – Dummy01 Mar 14 '12 at 13:35
  • ...Unless you changed the name of you startup class from App to something else – Dummy01 Mar 14 '12 at 13:38
  • That works most of the time. However, if the user has changed the standards for that language, then it won't work. Thus, you need to use a "culture-aware" binding, as in http://stackoverflow.com/questions/5831455/use-real-cultureinfo-currentculture-in-wpf-binding-not-cultureinfo-from-ietfl/5937477#5937477 – Daniel Rose Apr 19 '12 at 10:16
  • @Daniel Rose You mean if the user has changed the standards for that language during runtime of the application? Yes then you are right it will not work. As millions of other applications including the ones from Microsoft that read the regional settings once at startup. In any other case it will work just fine. – Dummy01 Apr 19 '12 at 10:24
  • No, if they have changed it at all. For example, if the user has the German culture, but changed the decimal separator to '.'. Then this will use for conversion the standard for the language (i.e. ','), but not the actual value (i.e. '.'). I am currently working on fixing our application to handle this BTW. – Daniel Rose Apr 19 '12 at 10:34
  • 1
    @Daniel Rose You are right... I just checked it myself... That is a great disappointment and of course it can happen. Thanks for "awaking" me up with this. – Dummy01 Apr 19 '12 at 11:10
-1

Did you tried

System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("de-DE");
System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo("de-DE");
Felix C
  • 1,755
  • 5
  • 26
  • 40