How to avoid implementing INotifyPropertyChanged manually

后端 未结 7 1748
名媛妹妹
名媛妹妹 2021-01-05 02:44

Is there some way to avoid this. I have a lot of classes that are bound to DataGridViews and they are just simple collection of properties with default getter and setter. So

7条回答
  •  南笙
    南笙 (楼主)
    2021-01-05 02:59

    Create a container base class, eg:

    abstract class Container : INotifyPropertyChanged
    {
      Dictionary values;
    
      protected object this[string name]
      {
        get {return values[name]; }
        set 
        { 
          values[name] = value;
          PropertyChanged(this, new PropertyChangedEventArgs(name));
        }
      }
    }
    
    class Foo : Container
    {
      public int Bar 
      {
        {get {return (int) this["Bar"]; }}
        {set { this["Bar"] = value; } }
      }
    }
    

    Note: very simplified code

提交回复
热议问题