Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TextBox bound to Decimal with UpdateSourceTrigger=PropertyChanged

I've been battling this issue for a while now and seem not to be able to come up with a concrete workaround - I have a TextBox which is bound to a decimal, and the binding has UpdateSourceTrigger set to PropertyChanged and is so by necessity (LostFocus won't work well in this case). The default behavior while I'm sure is somehow explainable, is not acceptable for my purposes, so I've tried the following StringFormat, which I had thought remedied the issue, but only partially and am now looking for something more concrete. My originaly fix was to add a string format to the binding...in my case it was

StringFormat={0:#.#####} 

so when typing something like .12345 or 1.5 the solution works great, however if I type .01234, as soon as I hit the zero key, it removes the decimal I had just typed...which for obvious reasons would be disasterous in terms of data entry. I'm hoping that my familiarity with string formatting is just lacking. Wost case scenario I'll have my exposed property be a string and the setter and getter just convert to decimal, but that seems like a hacky solution.

Thanks!

Aj

like image 444
Aaj Avatar asked Apr 27 '12 16:04

Aaj


1 Answers

I came across this because Im seeing the same issue with a .Net 4.6.2 build. I needed to enter an exchange rate, e.g. 1.15 and found it was ending up as 115 with the decimal removed. My workaround was to bind to a string property that maintained it's own string value while input e.g. 1, 1., 1.1, 1.15. But with each input attempted to update the underlying numeric property inside a Try Catch. It's far from elegant, but works for my needs.

like image 146
ac66 Avatar answered Oct 25 '22 22:10

ac66