王彦为
聚沙成塔
  1. 首页
  2. WPF
  3. 正文

WPF中TextBox在.NET 4.5无法输入浮点数

2016年12月16日 10236次浏览 50人点赞 2条评论

最近发现一个很奇怪的现象,TextBox中的Text绑定double型数据,触发条件UpdateSourceTrigger=PropertyChanged时,在.net4.5框架下无法输入小数点,而在.net 4.0之前的框架不存在这个问题。为了描述地更清晰,下面做一个简单的模型来说明。

现象描述

// .xaml
<Grid>
    <TextBox Text="{Binding Score,UpdateSourceTrigger=Default}"/>
</Grid>

// .cs
using System.ComponentModel;
public partial class Window1 : Window
{
    Student stu = new Student();
    public Window1()
    {
        InitializeComponent();
        this.DataContext = stu;
    }
}

// ViewMode
class Student : INotifyPropertyChanged
{
    private double score;
    public event PropertyChangedEventHandler PropertyChanged;
    protected void InvokePropertyChanged(string property)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs(property));
            }
        }

    /// <summary>
    /// 姓名
    /// </summary>
    public string Name { get; set; }

    /// <summary>
    /// 得分
    /// </summary>
    public double Score
    {
        get { return score; }
        set
        {
            if (score != value)
            {
                score = value;
                InvokePropertyChanged("Score");
            }
        }
    }
}

这是一个很简单的MVVM模型,相信熟悉WPF Binding的人都能看明白。下面是不同条件下的测试结果:

.NET框架UpdateSourceTrigger结果
.NET 4.0及以下版本Default能输入小数
.NET 4.0及以下版本PropertyChanged能输入小数
.NET 4.5及以上版本Default能输入小数
.NET 4.5及以上版本PropertyChanged不能输入小数

至此,这个现象已经描述清楚了,下面介绍原因和解决方法。

产生原因

据说转换器的问题,至于是微软有意为之还是bug就不得而知了。

解决方法

方法一:修改Xmal中的StringFormat,该方法的弊端就是每个绑定语句都要加StringFormat={}{0},如果StringFormat限定了格式,那么该方法不可用。

<Grid>
    <TextBox Text="{Binding Score,UpdateSourceTrigger=PropertyChanged,
        StringFormat={}{0}}"/>
</Grid>

方法二:可以在App.cs的入口函数中加入一行代码“FrameworkCompatibilityPreferences.KeepTextBoxDisplaySynchronizedWithTextProperty = false”

class App
{
    [STAThread]
    public static void Main(params string[] args)
    {
        FrameworkCompatibilityPreferences.KeepTextBoxDisplaySynchronizedWithTextProperty = false;
        Application app = new Application();
        app.Run(new Window1());
    }
}

// 或者
public partial class App : Application
{
    protected override void OnStartup(StartupEventArgs e)
    {
        FrameworkCompatibilityPreferences.KeepTextBoxDisplaySynchronizedWithTextProperty = false;
        base.OnStartup(e);
    }
}
标签: 暂无
最后更新:2020年09月27日

王彦为

准备考试,暂停更新一个月。

打赏 点赞
< 上一篇
下一篇 >

文章评论

  • 西姆

    刚好遇到这个问题,尝试了你的方法,在双向绑定的时候仍然有问题,最终我还是把UpdateSourceTrigger改为Default了。
    谢谢分享好文,我已转载本文,如果有任何不妥可以联系我撤下。

    2020年03月17日
    回复
  • 取消回复

    王彦为

    准备考试,暂停更新一个月。

    分类目录
    • WPF (5)
    • 嵌入式 (8)
    • 工具 (1)
    • 攻防 (2)
    • 杂谈 (2)
    最新 热点 随机
    最新 热点 随机
    版本控制工具之SVN的使用 玩转DragronBoard 410c系列之七:Linux系统使用QT Creator开发ARM应用程序 玩转DragronBoard 410c系列之六:Linux系统使用C语言控制GPIO 网站改版说明 玩转DragronBoard 410c系列之五:ARM交叉编译工具链介绍 玩转DragronBoard 410c系列之四:Linux系统搭建ARM应用开发环境
    信息安全的攻与防 WPF的TextBox输入验证之Exception验证 WPF中TextBox在.NET 4.5无法输入浮点数 WPF的TextBox输入验证之ValidationRule验证 XP运行WPF程序,部分控件透明 玩转DragronBoard 410c系列之二:远程访问Linux系统

    COPYRIGHT © 2020 王彦为. ALL RIGHTS RESERVED.

    苏ICP备16063331号-1