9

I have often used TextBox to bind to Integers without much problem.

However if I try to bind a TextBox to a Double it doesn't work.

When I type 5,85 ( , being my cultures decimalSeperator) I pass 585.0 to the double value.

How is it being converted and what solution could I use to fix this? Would a ValueConverter be the best solution?

Ingó Vals
  • 4,788
  • 14
  • 65
  • 113
  • 2
    This sounds like that either isn't your decimal separator or the application does not care about the culture. – H.B. Jan 13 '12 at 18:21
  • I was debugging and ensured `NumberFormatInfo.CurrentInfo.NumberDecimalSeparator` was what i though it was. That's the right property isn't ? – Ingó Vals Jan 13 '12 at 18:25

2 Answers2

4

You could try adding this to your application's constructor:

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

However, please note that this will not work if you customize the decimal separator. (WPF double valued data binding with custom decimal separator)

Community
  • 1
  • 1
friskm
  • 366
  • 3
  • 7
  • Why not simply set Language = XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.IetfLanguageTag) in the main window constructor? – Clemens Jan 13 '12 at 22:59
  • 1
    Clemens, Your solution works also but you have to remember to put it separately for each window you create in the application compared to the 'application contructor method' where the language is set for all frameworkelements in the application – friskm Jan 15 '12 at 10:28
  • You're right. Makes sense to override the default value of a built-in dependency property in this case. – Clemens Jan 15 '12 at 12:34
1

For diagnostic purposes, you can add these two lines of code to the start of your program...

    var cc = Thread.CurrentThread.CurrentCulture;
    var cuic = Thread.CurrentThread.CurrentUICulture;

And compare the results. The chances are quite good that the 'cuic' culture will hold 'en-US' because the UI thread is typically done that way. You can change this by setting the <UICulture> tag in the project file, or you can try as a diagnostic...

Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;

and assess the side effects. Otherwise you can implement an IValueConverter...

Gayot Fow
  • 8,710
  • 1
  • 35
  • 48