Two way databinding in winforms, Inotifypropertychanged implemented in base class

前端 未结 4 878
抹茶落季
抹茶落季 2020-12-20 16:50

I use .Net 3.5, Winforms, Databinding

I have derived classes, the base class implements IPropertychanged

    public event PropertyChangedEventHandle         


        
4条回答
  •  [愿得一人]
    2020-12-20 17:16

    I implemented the "INotifyPropertyChanged", but raise the PropertyChanged event only when the new value is different from the old value:

    public class ProfileModel : INotifyPropertyChanged
    {
        private Guid _iD;
        private string _name;
        public event PropertyChangedEventHandler PropertyChanged;
    
        public Guid ID
        {
            get => _iD;
            set
            {
                if (_iD != value)
                {
                    _iD = value;
                    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("ID"));
                }
            }
        }
    
        public string Name
        {
            get => _name;
            set
            {
                if (_name != value)
                {
                    _name = value;
                    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Name"));
                }
            }
        }
    }
    

    Now just bind to the controls:

    txtProfileID.DataBindings.Clear();
    txtProfileID.DataBindings.Add("Text", boundProfile, "ID", true, DataSourceUpdateMode.OnPropertyChanged);
    

提交回复
热议问题