Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF (.net 3.5) Binding to number (int, dbl) but allow user to input nothing

If you bind to a view-model property that is a nullable double (or int) how do you allow a user to specify nothing.

I'm looked into validation but I can't see how I could get that to help.

It seems like if a textbox is blanked out WPF sees it as an empty string and then doesn't put the into the vm proptery.

So 2 ways around it I can think of is:

  1. vm property is a string which is validated and updates the backing model.
  2. special code that indicates no value (in my case the dbl should not be neg so -9 could be a null...but don't like the idea of magic numbers)

Any other ideas? Am I missing something?

cody

like image 398
code Avatar asked Feb 26 '23 19:02

code


1 Answers

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

{Binding TargetNullValue=''}
like image 88
Quartermeister Avatar answered Apr 07 '23 02:04

Quartermeister