c# - 为什么绑定(bind)设置在 .NET 4 和 .NET 3.5 中的行为不同

标签 c# wpf xaml c#-4.0

我最近将一个应用程序从 VS 2008 .NET 3.5 项目转换为 VS2010 .NET 4 项目。项目中的某些 WPF 对话框在转换后表现不同。我想了解是什么导致了这种行为差异,这样我就可以找到并修复现在可能存在问题的其他区域。

例如,我有一个 MVVM 对话框,可以让用户输入一个数字。该数字在内部存储为 double ,如果用户键入的文本是有效的 double ,则用户只能接受该对话框。因此,我有一个文本框绑定(bind)到 ViewModel 中的一个字符串,还有一个仅在字符串为有效 double 值时才启用的确定按钮。相关的 Xaml 如下所示:

<TextBox Text="{Binding ValueString, UpdateSourceTrigger=PropertyChanged}"/>
<Button IsEnabled="{Binding ValueIsValid}">OK</Button>

ViewModel 看起来像:

class ViewModel : INotifyPropertyChanged
{
    private double actualValue;
    public string ValueString
    {
        get { return actualValue.ToString("G3"); }
        set
        {
            double doubleValue;
            if (double.TryParse(value, NumberStyles.Float, CultureInfo.CurrentCulture, out doubleValue))
            {
                actualValue = doubleValue;
                ValueIsValid = true;
                RaisePropertyChanged("ValueString");
            }
            else
            {
                ValueIsValid = false;
            }
        }
    }

    private bool valueIsValid = true;
    public bool ValueIsValid
    {
        get { return valueIsValid; }
        set
        {
            if (valueIsValid != value)
            {
                valueIsValid = value;
                RaisePropertyChanged("ValueIsValid");
            }
        }
    }

    private void RaisePropertyChanged(string property)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(property));
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
}

这在 .NET 3.5 中运行良好,但在 .NET 4 上运行时,当用户输入数字时会出现问题。例如,如果用户在.NET 3.5 版本的文本框中输入“3.05555”,则一切正常。但是在 .NET 4 版本中,他们可以正常输入 3.05,但是当他们键入下一个“5”时,文本框的值变为“3.06”,如果再次按 5,则文本框的值变为“3.07”。就好像值一被设置就从 ValueString 属性中读回(因此被格式化为“G3”),但这在 .NET 3.5 中并不常见.

我看过了 What's New in the .NET Framework 4 (包括 What's New in WPF Version 4 ),但我没有找到任何有关此更改的信息。

如果您想亲眼看看,我已经创建了一个小示例 VS2010 解决方案,您可以 download from here . BindingTest2008项目是从VS 2008转过来的,目标是.NET 3.5,BindingTest2010项目是在VS 2010中创建的,目标是.NET 4。两个项目的代码是一样的,但是.NET 4项目有这个问题。

如果能帮助我理解为什么会这样,我将不胜感激。 谢谢。

已更新:删除调用 RaisePropertyChanged("ValueIsValid"); 不会改变行为,输入无效数字(例如“3.1a”)也不会t 中间被最后一个有效数字替换(例如,在这种情况下为“3.1”)。也可以输入比 3 位有效数字更高的精度的数字。例如。 “3.0545555”- 问题似乎只发生在您刚刚输入的内容导致第 3 位有效数字四舍五入时。

最佳答案

造成这种行为差异的原因是:

In 3.5, the binding would write a new value back to the source after each keystroke, without changing the TextBox text. But that text might not represent the source's value accurately, perhaps because it doesn't include formatting and conversion, or because the source changed the value (in the property-setter) to something else. This led to frequent and vehement complaints - people wanted the TextBox to show the source's value, exactly as a TextBlock would if bound to the same property with the same converters and formatting. The UI should display what's actually in the data, not what the end-user typed.

To fix this class of bugs in 4.0, the binding now applies formatting and conversion to the source's new value after every update. (LostFocus bindings already did this in 3.5.) The TextBox now shows what's in the data, but that can make the user's typing more complex.

We plan to improve this scenario in the next release in at least two ways: 1. When the TextBox text is replaced with a revised string, the insertion point (cursor) that worked for the old string may no longer be correct for the new string. The heuristic that guesses where to put the cursor can be improved. 2. Bindings will expose a way to do LostFocus (or Explicit) updates with partial validation after each keystroke. The formatting/conversion only gets applied when focus changes, but the user gets validation feedback after every keystroke.

  • Sam (WPF team)

来自“Changed behaviour from .Net 3.5 to .Net 4.0 of WPF TextBox formatting when PropertyChanged is used as UpdateSourceTrigger

关于c# - 为什么绑定(bind)设置在 .NET 4 和 .NET 3.5 中的行为不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4712917/

相关文章:

c# - WPF 的轻型文本库?

c# - wpf中带有复选框通用控件的多选组合框

xaml - 将文本 block 与页面底部对齐

windows - x :Uid generator for Windows 8 projects

c# - 在 WinRT 应用程序中处理捏合、缩放、旋转手势

c# - 自动编译两个版本的C#程序

c# - 具有 10 英尺 gui 的多个文本元素的 WPF 按钮

c# - Lambda 实现接口(interface)

c# - Visual Studio 2010 : Metadata file ".../Debug/Graph.dll" could not be found

c# - Model和ViewModel关系和继承-应该如何实现?