10

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

4

2 回答 2

1

我遇到了这个问题,因为我在 .Net 4.6.2 版本中看到了同样的问题。我需要输入一个汇率,例如 1.15,发现它以 115 结束,小数点被删除。我的解决方法是绑定到一个字符串属性,该属性在输入时保持它自己的字符串值,例如 1、1.、1.1、1.15。但是每个输入都尝试更新 Try Catch 中的基础数字属性。它远非优雅,但可以满足我的需求。

于 2018-07-02T19:42:40.107 回答
0

你有两个简单的选择来实现你想要的:

  1. 尝试先输入“0”...您可以毫无问题地输入“0.01234”。

  2. 改用StringFormat={}{0:0.00000}...然后您可以毫无问题地输入“.01234”。

于 2013-07-31T08:15:03.740 回答