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

21,014

Solution 1

The reason why all properties are being read when the event gets fired rests in the PushData method called on the binding object when the ProperyChanged event is fired. If you look at the stacktrace, you will notice that the PropValueChanged method of the internal object BindToObject is called, that in turn calls the Oncurrentchanged event on the BindingManager. The binding mechanism keeps track of the current item changes, but it doesn't do a more granular distinction. The "culprit" PushData method calls the getter on your properties (take a look at the code using reflector). So there is no way around it. That being said, as a rule of thumb, in the get and set accessors it is not recommended to do heavy processing, use separate get and set methods for that (if possible)

Also take a look at this article, and this comment in particular (http://www.codeproject.com/Messages/2514032/How-Binding-watches-control-properties-i-e-how-doe.aspx), that explains exactly how the propertychanged event gets fired, though it will not address your getter problem: http://www.codeproject.com/KB/database/databinding_tutorial.aspx?msg=2514032

An idea to explore is to delay the getter being called. You can achieve this by playing around with the ControlUpdateMode property of the binding. When this value is set to Never, the corresponding control will not update when there is a change. However, when you switch the value back to OnPropertyChanged, PushData method will be called, so the getters will be accessed. So considering your example this code will temporary prevent the textbox 2 to update:

void but_Click(object sender, EventArgs e)
        {                   
            txt2.DataBindings[0].ControlUpdateMode = ControlUpdateMode.Never;
            _presenter.SomeText1 = "some text 1";
        }

Solution 2

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.

Share:
21,014
Dave Welling
Author by

Dave Welling

.NET SQL NetCF JS

Updated on March 08, 2020

Comments

  • Dave Welling
    Dave Welling about 4 years

    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 property changed. (See example code below)

    This seems absurdly wasteful since the interface requires the name of the changing property. It is causing a lot of clocking in my app because some of the property getters require calculations to be performed.

    I'll likely need to implement some sort of logic in my getters to discard the unnecessary reads if there is no better way to do this.

    Am I missing something? Is there a better way? Don't say to use a different presentation technology please -- I am doing this on Windows Mobile (although the behavior happens on the full framework as well).

    Here's some toy code to demonstrate the problem. Clicking the button will result in BOTH textboxes being populated even though one property has changed.

    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.Windows.Forms;
    
    namespace Example
    {
    public class ExView : Form
    {
        private Presenter _presenter = new Presenter();
        public ExView()
        {
            this.MinimizeBox = false;
    
            TextBox txt1 = new TextBox();
            txt1.Parent = this;
            txt1.Location = new Point(1, 1);
            txt1.Width = this.ClientSize.Width - 10;
            txt1.DataBindings.Add("Text", _presenter, "SomeText1");
    
            TextBox txt2 = new TextBox();
            txt2.Parent = this;
            txt2.Location = new Point(1, 40);
            txt2.Width = this.ClientSize.Width - 10;
            txt2.DataBindings.Add("Text", _presenter, "SomeText2");
    
            Button but = new Button();
            but.Parent = this;
            but.Location = new Point(1, 80);
            but.Click +=new EventHandler(but_Click);
        }
    
        void but_Click(object sender, EventArgs e)
        {
            _presenter.SomeText1 = "some text 1";
        }
    }
    
    public class Presenter : INotifyPropertyChanged
    {
    
        public event PropertyChangedEventHandler PropertyChanged;
    
        private string _SomeText1 = string.Empty;
        public string SomeText1
        {
            get
            {
                return _SomeText1;
            }
            set
            {
                _SomeText1 = value;
                _SomeText2 = value; // <-- To demonstrate that both properties are read
                OnPropertyChanged("SomeText1");
            }
        }
    
        private string _SomeText2 = string.Empty;
        public string SomeText2
        {
            get
            {
                return _SomeText2;
            }
            set
            {
                _SomeText2 = value;
                OnPropertyChanged("SomeText2");
            }
        }
    
        private void OnPropertyChanged(string PropertyName)
        {
            PropertyChangedEventHandler temp = PropertyChanged;
            if (temp != null)
            {
                temp(this, new PropertyChangedEventArgs(PropertyName));
            }
        }
    }
    

    }