Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binding Double to TextBox

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?

like image 974
Ingó Vals Avatar asked Jan 13 '12 18:01

Ingó Vals


2 Answers

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)

like image 81
friskm Avatar answered Nov 07 '22 12:11

friskm


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...

like image 1
Gayot Fow Avatar answered Nov 07 '22 11:11

Gayot Fow