Implementing INotifyPropertyChanged

It's changed a lot over the years. Here is the latest iteration that I use.

   1 private ObservableCollection<string> _Area = new ObservableCollection<string>();
   2 public ObservableCollection<string> Area
   3 {
   4     get => _Area;
   5     set => SetField(ref _Area, value);
   6 }
   7 
   8 //...
   9 
  10 public event PropertyChangedEventHandler PropertyChanged;
  11 
  12 protected void SetField<T>(ref T field, T value, [CallerMemberName] string propertyName = null)
  13 {
  14     field = value;
  15     PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  16 }

I used to use something like this:

   1 public int test {
   2     get 
   3     {
   4         return _test;
   5     }
   6     set
   7     {
   8         _test = value;
   9         OnPropertyChanged();
  10     }
  11 }
  12 
  13 //...
  14 
  15 protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
  16 {
  17     if (PropertyChange != null) 
  18     {
  19         PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
  20     }
  21 }

And before that we just passed in the name. It was kind of a pain.

ProgrammingLinks/INotifyPropertyChanged (last edited 2019-03-04 22:59:40 by scot)