Why binding configuration behaves differently in .NET 4 and .NET 3.5

I have an application that I recently converted from a VS 2008.NET 3.5 project to a VS2010.NET 4 project. Some of the WPF dialogs in the project behave differently after the conversion. I would like to understand what causes this difference in behavior, so I can find and fix other areas that may now have problems.

As an example, I have an MVVM dialog that allows a user to enter a number. The number is stored internally as a double, and the user can only accept a dialogue if the text they are typing is a real double. Therefore, I have a text field associated with a row in the ViewModel, and an OK button that is enabled only if the row is a valid double. The corresponding Xaml is as follows:

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

And the ViewModel looks like this:

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, , . , "3.05555" .NET 3.5, . .NET 4 3.05, "5", "3.06", "3.07", 5. ValueString, ( , "G3" ), .NET 3.5.

.NET Framework 4 ( WPF 4), .

, VS2010, . BindingTest2008 VS 2008 .NET 3.5, BindingTest2010 VS 2010, .NET 4. , .NET 4 .

, . .

. RaisePropertyChanged("ValueIsValid"); (, "3.1a" ) (, "3.1" ). , 3 . . "3.0545555" - , , , 3- .

+3
2

:

3.5 , TextBox. , , , ( ) - . - TextBox, , , TextBlock, . , , , .

4.0, . (LostFocus 3.5.) TextBox , , .

: 1. TextBox , point (), . , , . 2. LostFocus ( ) . / , .

  • ( WPF)

" .Net 3.5 .Net 4.0 WPF TextBox, PropertyChanged UpdateSourceTrigger"

+5

, :

 get { return actualValue.ToString("G3"); }

.Net 4 , G3, , 3 (3.055 3.06).

3.5 4 , , -, . 3.5, PropertyChanged, ( ). .Net 4 , PropertyChanged , , .. IS , , , .

+2

Source: https://habr.com/ru/post/1785895/


All Articles