WPF (.net 3.5) is a number binding (int, dbl), but allows the user to enter anything

If you bind to a view-model property, which is a nullable double (or int), how do you allow the user to not specify anything.

I am studying validation, but I can’t figure out how to help me.

It seems that if the text field is obscured, WPF sees it as an empty string, and then does not put it in the vm version.

Thus, I can think of two ways:

  • The vm value is the string that validated and updated the support model.
  • special code that does not indicate a value (in my case, dbl should not be negative, so -9 may be null ... but I don't like the idea of ​​magic numbers)

Any other ideas? Did I miss something?

Cody

+4
source share
1 answer

Set TargetNullValue to an empty binding string. When WPF sees this value, it will set the source to null instead of trying to convert the value.

{Binding TargetNullValue=''} 
+8
source

Source: https://habr.com/ru/post/1313804/


All Articles