将 Double 绑定到 TextBox

发布于 2024-12-27 01:37:03 字数 194 浏览 3 评论 0原文

我经常使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

柠檬色的秋千 2025-01-03 01:37:03

您可以尝试将其添加到应用程序的构造函数中:

FrameworkElement.LanguageProperty.OverrideMetadata(typeof(FrameworkElement),
             new FrameworkPropertyMetadata(XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.IetfLanguageTag)));

但是,请注意,如果您自定义小数分隔符,这将不起作用。 (WPF 双值数据与自定义小数分隔符绑定

You could try adding this to your application's constructor:

FrameworkElement.LanguageProperty.OverrideMetadata(typeof(FrameworkElement),
             new FrameworkPropertyMetadata(XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.IetfLanguageTag)));

However, please note that this will not work if you customize the decimal separator. (WPF double valued data binding with custom decimal separator)

潜移默化 2025-01-03 01:37:03

出于诊断目的,您可以将这两行代码添加到程序的开头...

    var cc = Thread.CurrentThread.CurrentCulture;
    var cuic = Thread.CurrentThread.CurrentUICulture;

并比较结果。 “cuic”文化很有可能保留“en-US”,因为 UI 线程通常是这样完成的。您可以通过在项目文件中设置 标记来更改此设置,或者您可以尝试作为诊断...

Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;

并评估副作用。否则你可以实现一个 IValueConverter...

For diagnostic purposes, you can add these two lines of code to the start of your program...

    var cc = Thread.CurrentThread.CurrentCulture;
    var cuic = Thread.CurrentThread.CurrentUICulture;

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...

Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;

and assess the side effects. Otherwise you can implement an IValueConverter...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文