验证@ WPF Textbox后删除了小数

时间:2012-03-14 12:57:30

标签: c# wpf validation comma

我在Code behinde中创建一个Textbox并将其绑定到double属性。

                TextBox t = new TextBox();
                t.Width = 80;
                t.DataContext = s;         
                Binding binding = new Binding();
                binding.Mode = BindingMode.TwoWay;
                binding.Path = new PropertyPath("Value");
                BindingOperations.SetBinding(t, TextBox.TextProperty, binding);

当我输入45,45(逗号)之类的值时,它被解析为4545。

如果我输入45.45(点),则用45,45解析它是正确的。

我使用德语设置,我的小数Sperator是,

我做错了什么?

3 个答案:

答案 0 :(得分:3)

尝试将绑定。ConverterCulture设置为目标文化。

例如

 binding.ConverterCulture = CultureInfo.CurrentCulture;

答案 1 :(得分:2)

一个明确的,而不是特定于文化的解决方案是将其添加到App.xaml.cs中,通常WPF将始终使用正确的文化 - 区域设置:

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

答案 2 :(得分:-1)

你试过吗

System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("de-DE");
System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo("de-DE");
相关问题