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

  最近发现一个很奇怪的现象,TextBox中的Text绑定double型数据,触发条件UpdateSourceTrigger=PropertyChanged时,在.net4.5框架下无法输入小数点,而在.net 4.0之前的框架不存在这个问题。

解决办法

  修改Xmal中的StringFormat

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

  如果不想在每一条TextBox后都加StringFormat={}{0}这条代码,可以在App.cs的入口函数中加入一行代码即可

class App
{
    [STAThread]
    public static void Main(params string[] args)
    {
     //设置一个值,此值指示数据绑定 TextBox 是否应显示与源的 Text 属性值一致的字符串 为false System.Windows.FrameworkCompatibilityPreferences.KeepTextBoxDisplaySynchronizedWithTextProperty
= false; Application app = new Application(); app.Run(new Window1()); } }
 
原文地址:https://www.cnblogs.com/applebox/p/12228896.html