10
votes

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

2
I feel your pain. I and (we, in various projects) ended up creating a NumericTextBox UserControl that was able to handle the situation you describe as well, as other interesting inputs: int vs double, handling negative values, decimals, validation (numeric vs alpha) etc. Depending on how robust your textbox has to be, I would recommend creating your own NumericTextBox that inherits from TextBox and add your rules. Good luck!denis morozov
@denismorozov - while I didn't come up with a legit solution, I did read another article where someone suggested setting FocusManager.IsFocusScope="False" on the button to take focus away from the textbox, which in my case allows me to switch from using property changed back to the default of lost focus because it will execute my binding prior to the button's command execution. It may not be a solution for you, but it worked for me at least in this instance.Aaj
hmm, interesting, I will try that!denis morozov
@denismorozov - an addition to that, if you don't want your button to keep the focus after click, you can use the FocusManager.IsFocusScope in conjunction with FocusManager.FocusedElement and set it to a control that can receive focus...this way your button doesn't stay highlighted after click...also makes a nice way to move focus in other instances where it would be helpful.Aaj

2 Answers

1
votes

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.

0
votes

You have two simple choices to achieve what you want:

  1. Try typing a '0' first... you can enter '0.01234' without issue.

  2. Use StringFormat={}{0:0.00000} instead... then you can enter '.01234' without issue.