I’ve got a TextBox that is driving me insane with the way it is auto-formatting itself as I enter numbers.
For example, I want to enter -0.00004, but as I type, the TextBox removes the negative sign as soon as I type the first 0.
I then tried -.00004, which doesn’t remove the negative sign when I type ., but then as soon as I press 0 it removes the - and ..
The only way I’ve been able to get the TextBox to behave is to enter 0.00004 first, which it then autoformats to 4E-05, then add the negative sign to the front. Or I can enter -4E-5, which it then autoformats to -4E-05, which is fine, but if I enter -4E-05 the E gets dropped as soon as I press 0.
What I’m hoping for is a way to disable this behavior with a built-in property of some kind, but if it does not exist, writing an IValueConverter might be my only option.
One way to work around the auto-formatting is to use an
IValueConverteron the bound double property. You will need to add logic to theConvertmethod to convert from a double to a string and theConvertBackmethod to convert from a string to a double. Here’s the MSDN article with an example.Another way would be to set the
StringFormaton the Binding to a number with the number of decimal places to show. Take a look here for other formats.