Hi,

I wonder if anybody can set me straight as I am having a hard time understanding what is going on with INotifyPropertyChanged.

My understanding of the events are:

1> Have a window with 2 textfields and a button
2> Create an instance of the Employee object
5> Bind both textboxes to the Employee instance
6> The button just does ((Employee)_bs.Current).Name = "Bill"

Here is the Employee class:

public class Employee : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler propertyChanged;
    public string Name
    {
        get { return _name; }
        set 
        {
            if (_name != value)
            {
                _name = value;
                //notifyPropertyChanged();
            }
        }
    }
    public void notifyPropertyChanged([CallerMemberName] string property)
    {
        if (propertyChanged != null)
            propertyChanged(this, new PropertyChangedEventArgs(property));
    }
}

Now when I type something in textbox1 and lose focus, textbox2 is altered but when I click the button neither textbox is updated.

Obviously if I uncomment the call to notifyPropertyChanged all works fine. This however brings up questions in my mind.

1> If I change textfield1 this must be changing the datasource and then the datasource somehow triggers textbox2 to re-fetch the data then displaying it.

2> If I click the button this changes the datasource but for some reason the control isn't triggered to fetch the data again.

3> If when I uncheck the notifyPropertyChanged line, it works, but when using events I would normally assign a method to an event ie. EmployeeInstance.PropertyChanged += myMethod, and this method would update controls/properties etc.

So is there something going on in the background that I am just unaware of, if so what? Otherwise something is going wrong in my head!

I hope that I have explained myself well enough and someone can put me straight.

Thanks

Hmm, that's interesting... When textBox1 loses focus, are you giving it to textBox2? I would imagine (but never tried it) that when a TextBox gains focus it would reevaluate the property. I'm on my cell, so i can't test anything at the moment.

As for number 3 the handling of the PropertyChanged event is managed by the .NET Binding classes.

Now when I type something in textbox1 and lose focus, textbox2 is altered but when I click the button neither textbox is updated.

Obviously if I uncomment the call to notifyPropertyChanged all works fine. This however brings up questions in my mind.

  1. If I change textfield1 this must be changing the datasource and then the datasource somehow triggers textbox2 to re-fetch the data then displaying it.

The default datasource update mode is . When you leave the textbox you trigger its validation. If you set the TextBox.CauseValidation property to false, you would not see the second textbox update to reflect the change to the datasource.

If you use the longer Add method overload and specify "OnPropertyChanged" for the update mode, you will see the other textbox change as you type. "OnValidation" and "OnPropertyChanged" are for the control and its property to which you are adding the binding, not the datasource.

I'm not sure about this but it makes sense to me. If one databound control fires an event to update the datasource, then the binding manager does not require the property change event from the datasource (as it is the source of the change) to know that it needs to update all other controls bound to the same datasource.

  1. If I click the button this changes the datasource but for some reason the control isn't triggered to fetch the data again.

As you have disabled the property change notification, the binding manager has no way to know that the data source has changed and that the new value needs to be pushed out to all bound controls.

3> If when I uncheck the notifyPropertyChanged line, it works, but when using events I would normally assign a method to an event ie. EmployeeInstance.PropertyChanged += myMethod, and this method would update controls/properties etc.

You could do this, but that is replicating the work you have asked the databinding to do.
The databinding class does all the monitoring of the required events to accomplish the "magical" updates.:) As long as the datasource class and the control class plays by the rules, then all should work fine. If one of them breaks the contract of event notification, as you have forced by commenting out the notifyPropertyChanged, then things will not work as promised.

Thanks for your replies.

TnTinM thanks for the heads up on the controls CauseValidation and Add stuff, seems you were spot on there.

I imagine the 'magic' that you talk of in the controls binding class is something like:

Employee.PropertyChanged += (a,b) => control.Text = a.GetProperty(b}.GetValue()

This would make sense to me, what do you think?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.