Best Way to Implement Inotifypropertychanged

Asked

Viewed 25 times

0

The traditional way would be to create a class where implements INotifyPropertyChanged and then inherit where you use it:

public class NotifyPropertyChange : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    public void RaisePropertyChanged(string propertyname)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyname));
    }               
}

And then using:

private string _name;
public string Name { 
     get => _name; 
     set 
     { 
         RaisePropertyChanged(nameof(Name));
         _name = value; 
     }
 }

Another way would be using the Fody adding Inotifypropertychanged "automatically" by simply creating a class with INotifyPropertyChanged and inheriting it. (which is the current one I use)

And last I met Updatecontrols:

First you involve the class with ForView.Wrap()

public partial class Window1 : Window
{
    public Window1()
    {
        InitializeComponent();
        DataContext = ForView.Wrap(new PersonPresentation(new Person()));
    }
}

And then use the Independent on the property you want to be notified of.

public class Person
{
    private string _firstName;
    private string _lastName;
    private int _displayStrategy;

    #region Independent properties
    // Generated by Update Controls --------------------------------
    private Independent _indDisplayStrategy = new Independent();
    private Independent _indFirstName = new Independent();
    private Independent _indLastName = new Independent();

    public string FirstName
    {
        get { _indFirstName.OnGet(); return _firstName; }
        set { _indFirstName.OnSet(); _firstName = value; }
    }

    public string LastName
    {
        get { _indLastName.OnGet(); return _lastName; }
        set { _indLastName.OnSet(); _lastName = value; }
    }

    public int DisplayStrategy
    {
        get { _indDisplayStrategy.OnGet(); return _displayStrategy; }
        set { _indDisplayStrategy.OnSet(); _displayStrategy = value; }
    }
    // End generated code --------------------------------
    #endregion
}

So I was in doubt, not just between these two, but overall, currently, which is the best out to notify the View?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.