How to avoid implementing INotifyPropertyChanged manually

后端 未结 7 1718
名媛妹妹
名媛妹妹 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 03:18

    I have just found ActiveSharp - Automatic INotifyPropertyChanged, I have yet to use it, but it looks good.

    To quote from it's web site...


    Send property change notifications without specifying property name as a string.

    Instead, write properties like this:

    public int Foo
    {
        get { return _foo; }
        set { SetValue(ref _foo, value); }  // <-- no property name here
    }
    

    Note that there is no need to include the name of the property as a string. ActiveSharp reliably and correctly figures that out for itself. It works based on the fact that your property implementation passes the backing field (_foo) by ref. (ActiveSharp uses that "by ref" call to identify which backing field was passed, and from the field it identifies the property).

提交回复
热议问题