Create a class suitable for property change notification

To implement data binding in WPF, according to MS, "the class must ensure that notification properties are correctly changed." [ here)

AFAIK, the part of setting it up means taking the following steps if they are not set up in the class yet (ref this article on MSDN)

  • When the properties are changed, they need to call the method to create the event.
    • This means that automatically implemented properties must be changed, therefore they use a personal support field, therefore, they setcan change the property and also call a method to create an event.
  • The class must implement INotifyPropertyChanged.
  • The class must declare a PropertyChangedEventHandler event.
  • The event should be raised something like this:

...

// Create the OnPropertyChanged method to raise the event 
protected void OnPropertyChanged(string name)
{
    PropertyChangedEventHandler handler = PropertyChanged;
    if (handler != null)
    {
        handler(this, new PropertyChangedEventArgs(name));
    }
}

If I already have many existing classes that do not take any of these steps, what is the best way to change these classes so that they only change as much as necessary to conform to these standards?

+4
source share
5 answers

If you need a solution with the smallest possible code possible, then you want a Fody PropertyChanged weaver . It is installed as a NuGet package with Install-Package PropertyChanged.Fodyor through the VS Package Manager dialog.

[ImplementPropertyChanged], . , , , . !

, , , " " .

+2

[CallerMemberName] # 5,

protected void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
{
    ...
}

CallerMemberName.

:

public DateTime Time
{
    get { return this.model.DateTime; }
    set
    {
        this.model.DateTime = value;
        NotifyPropertyChanged();
    }
}
+3

class IWillNotifyYou : INotifyPropertyChanged
{
    private int _firstProperty;
    public int FirstProperty
    {
        get { return _firstProperty; }
        set
        {
            if (value != _firstProperty)
            {
                _firstProperty = value;
                OnPropertyChanged("FirstProperty");
            }
        }
    }

    private string _secondProperty;
    public string SecondProperty
    {
        get { return _secondProperty; }
        set
        {
            if (value != _secondProperty)
            {
                _secondProperty = value;
                OnPropertyChanged("SecondProperty");
            }
        }
    }

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


    public event PropertyChangedEventHandler PropertyChanged;
}

, , FirstPropertyChanged

+1

": INotifyPropertyChanged" :

Public class Test : INotifyPropertyChanged 
{
}

ReSharpers ( ReSharper ).

:

OnPropertyChanged("PropertyName")

, XAML .

0

, . WPF MVVM (Model-View-ViewModel). , ( ).

, Business Objects, , INotifyPropertyChanged. WPF ViewModel. ViewModel - , View ( WPF) Model.

Model INotifyPropertyChanged, . ViewModel , ( ), , , , , -.

0

All Articles