Ingó Vals
Ingó Vals

Reputation: 4898

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?

Upvotes: 9

Views: 4046

Answers (2)

Gayot Fow
Gayot Fow

Reputation: 8792

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

Upvotes: 1

friskm
friskm

Reputation: 366

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)

Upvotes: 4

Related Questions