0

I have a textbox in my WPF UI where the user can input double values. This textbox is bound to a double value in my data model. I want to show a validation error when the user enters three or more decimal places into the textbox. I count zeros as decimal places as well. So a number like 1.000 or 0.000 shall trigger the validation error as well.

I want to do the validation logic with a SelfValidation ruleset in the bound class (so no external validation). The problem is that I cannot get the original user input from the textbox, because it is converted into a double which I work with. So when the user enters 1.000 I get 1.0 as a value to validate.

How can I find out how many decimal places the user originally entered so that my class can validate as desired? Can I solve this with a converter which somehow persists the information of original number of decimal digits?

user975561
  • 514
  • 1
  • 6
  • 17
  • You can try to use a DoubleToString converter. the string will keep your zeros trail and in the convert back you can remove them if you like. – Rom Oct 18 '16 at 11:09
  • 5
    If all you get is a `double`, you have no way to find out how many decimal places it has, let alone how many places the user has entered. You need to get access to the original input to do this validation. – Sergey Kalinichenko Oct 18 '16 at 11:11
  • Would this help? http://stackoverflow.com/questions/19539492/wpf-textbox-validation-c-sharp – AzzamAziz Oct 18 '16 at 12:04

0 Answers0