将 Double 绑定到 TextBox
我经常使用 TextBox 绑定到整数,没有太大问题。
但是,如果我尝试将 TextBox 绑定到 Double 则不起作用。
当我输入 5,85 ( ,作为我的文化小数分隔符)时,我将 585.0 传递给双精度值。
它是如何转换的以及我可以使用什么解决方案来解决这个问题? ValueConverter 是最好的解决方案吗?
I have often used TextBox to bind to Integers without much problem.
However if I try to bind a TextBox to a Double it doesn't work.
When I type 5,85 ( , being my cultures decimalSeperator) I pass 585.0 to the double value.
How is it being converted and what solution could I use to fix this? Would a ValueConverter be the best solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以尝试将其添加到应用程序的构造函数中:
但是,请注意,如果您自定义小数分隔符,这将不起作用。 (WPF 双值数据与自定义小数分隔符绑定 )
You could try adding this to your application's constructor:
However, please note that this will not work if you customize the decimal separator. (WPF double valued data binding with custom decimal separator)
出于诊断目的,您可以将这两行代码添加到程序的开头...
并比较结果。 “cuic”文化很有可能保留“en-US”,因为 UI 线程通常是这样完成的。您可以通过在项目文件中设置
标记来更改此设置,或者您可以尝试作为诊断...并评估副作用。否则你可以实现一个 IValueConverter...
For diagnostic purposes, you can add these two lines of code to the start of your program...
And compare the results. The chances are quite good that the 'cuic' culture will hold 'en-US' because the UI thread is typically done that way. You can change this by setting the
<UICulture>
tag in the project file, or you can try as a diagnostic...and assess the side effects. Otherwise you can implement an IValueConverter...