Binding Double to TextBox

佐手、 提交于 2019-12-08 16:21:08

问题


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?


回答1:


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)




回答2:


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



来源:https://stackoverflow.com/questions/8855272/binding-double-to-textbox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!