.NET WinForms INotifyPropertyChanged updates all bindings when one is changed. Better way?

后端 未结 2 553
清酒与你
清酒与你 2020-12-25 12:44

In a windows forms application, a property change that triggers INotifyPropertyChanged, will result in the form reading EVERY property from my bound object, not just the pro

2条回答
  •  别那么骄傲
    2020-12-25 13:11

    I'm testing subclassing binding like this and managing OnPropertyChanged, maybe helps you.

    public class apBinding : Binding
    {
    
            public apBinding(string propertyName, INotifyPropertyChanged dataSource, string dataMember)
                : base(propertyName, dataSource, dataMember)
            {
                this.ControlUpdateMode = ControlUpdateMode.Never;
                dataSource.PropertyChanged += new PropertyChangedEventHandler(OnPropertyChanged);
            }
    
            private void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
            {
    
                if (e.PropertyName == this.BindingMemberInfo.BindingField)
                {
                     this.ReadValue();
                }
            }
        }
    

    Now the problem that i find is that the control overwrites the value of the linked object, so i modified to

    public class apBinding : Binding
    {
    
            public apBinding(string propertyName, INotifyPropertyChanged dataSource, string dataMember)
                : base(propertyName, dataSource, dataMember)
            {
    
                dataSource.PropertyChanged += new PropertyChangedEventHandler(OnPropertyChanged);
            }
    
            private void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
            {
                this.ControlUpdateMode = ControlUpdateMode.Never;
                if (e.PropertyName == this.BindingMemberInfo.BindingField)
                {
                     this.ReadValue();
                }
            }
        }
    

    then the first time propertychanges is called i disable controlupdate. and the control is correctly updated at the first run.

提交回复
热议问题