强迫一个WPF文本框不工作了在.NET 4.0中 [英] Coerce a WPF TextBox not working anymore in .NET 4.0

查看:179
本文介绍了强迫一个WPF文本框不工作了在.NET 4.0中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的WPF应用程序我有一个文本框,用户可以输入一个百分比(如int,1和100之间)。 Text属性是数据绑定到一个视图模型,在那里我强迫值是在给定范围内,在二传手的一个属性。

In my WPF application I have a TextBox where the user can enter a percentage (as int, between 1 and 100). The Text property is databound to a property in a ViewModel, where I coerce the value to be in the given range in the setter.

不过,在.NET 3.5中,数据是不正确的UI被胁迫后显示。在<一个href="http://social.msdn.microsoft.com/forums/en-US/wpf/thread/c404360c-8e31-4a85-9762-0324ed8812ef/">this后在MSDN ,WPF博士指出,你必须手动更新绑定,正确的将被显示。所以,我有一个框TextChanged 处理程序(在视图),其中要求 UpdateTarget()。在code:

However, in .NET 3.5, the data is not shown properly in the UI after being coerced. In this post on MSDN, Dr. WPF states that you have to manually update the binding so the correct will be shown. Therefore, I have a TextChanged handler (in the View) which calls UpdateTarget(). In code:

查看XAML:

<TextBox Text="{Binding Percentage, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, TargetNullValue={x:Static sys:String.Empty}}"
    TextChanged="TextBox_TextChanged"/>

查看codebehind:

View codebehind:

private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
    // Removed safe casts and null checks
    ((TextBox)sender).GetBindingExpression(TextBox.TextProperty).UpdateTarget();
}

视图模型:

private int? percentage;
public int? Percentage
{
    get
    {
        return this.percentage;
    }

    set
    {
        if (this.Percentage == value)
        {
            return;
        }

        // Unset = 1
        this.percentage = value ?? 1;

        // Coerce to be between 1 and 100.
        // Using the TextBox, a user may attempt setting a larger or smaller value.
        if (this.Percentage < 1)
        {
            this.percentage = 1;
        }
        else if (this.Percentage > 100)
        {
            this.percentage = 100;
        }
        this.NotifyPropertyChanged("Percentage");
    }
}

不幸的是,这个code打破了.NET 4.0(同code,只是改变TargetFramework为4.0)。具体地说,在我裹挟着价值,为第一次,文本框忽略任何进一步的强制值,只要我继续输入整数值(因为我绑定到一个int)。

Unfortunately, this code breaks in .NET 4.0 (same code, simply changed TargetFramework to 4.0). Specifically, after I coerced the value for the first time, the TextBox ignores any further coerced values as long as I continue to enter integer values (since I am binding to an int).

所以,如果我输入123,后3我看到了值100。现在,如果我输入4,在视图模型的设定器得到值1004,它所胁迫到100。TextChanged事件然后火灾(和发件人的TextBox.Text是100),但是文本框上显示 1004。如果我再输入5,二传手得到值10045,等等。

So if I enter "123", after the 3 I see the value "100". Now if I enter "4", the setter in the ViewModel gets the value "1004", which it coerces to 100. The TextChanged event then fires (and the sender's TextBox.Text is "100"!), but the TextBox shows "1004". If I then enter "5", the setter gets the value "10045", etc.

如果我再输入一个a,顿时文本框显示正确的值,即100。如果我继续输入数字直到INT溢出会发生同样现象。

If I then enter an "a", suddenly the TextBox shows the correct value, i.e. "100". The same occurs if I continue to enter numbers until the int overflows.

我该如何解决这个问题?

How can I fix this?

推荐答案

尝试使用XAML中显式的,而不是的PropertyChanged:

Try using in xaml Explicit instead of PropertyChanged:

<TextBox Text="{Binding Percentage, Mode=TwoWay, UpdateSourceTrigger=Explicit, TargetNullValue={x:Static System:String.Empty}}"
             TextChanged="TextBox_TextChanged" />

和后面UpdateSource而不是UpdateTarget code

and in code behind UpdateSource instead of UpdateTarget

private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
    {
        // Removed safe casts and null checks
        ((TextBox)sender).GetBindingExpression(TextBox.TextProperty).UpdateSource();
    }

测试,它的工作原理。 顺便说一下这个问题可能会在.NET的更高版本可以解决。

Tested it and it works. Btw this problem will probably be resolved in a later version of .NET.

这篇关于强迫一个WPF文本框不工作了在.NET 4.0中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆