.net – Two-way databinding in .NET does not work, even with INotifyPropertyChanged objects

data-bindingnetwinforms

I'm working on a Windows Forms application, which of course involves databinding to objects.

From what I've gathered, two-way databinding does not work out-of-the-box with .NET.

My problem is that I can't find consistent information on implementing it :

  • According to ".NET WinForms in a nutshell", I have to create an event for each property, called <propertyName>Changed, and fire it in the setter of the associated property.

  • According to online sources (including MSDN), I simply need to implement INotifyPropertyChanged in my class, and fire the PropertyChanged event in all setters, passing in the name of the property.

However, assuming I have a simple UI with a textbox databound to a property of a class (implementing either of the above methods), if I set the value of the databound property in code, the UI does not reflect the change, using either methods.

So what am I missing here ?

Edit

I should have mentionned that INotifyPropertyChanged is not implemented in the declaration of the concerned classes, but through dynamic code generation roughly equivalent to DynamicProxy. I'm creating a new question for that topic since the answer fits the actual question.

Another edit INotifyPropertyChanged is properly implemented by the code generation tool, I have checked that point by casting an instance of the proxy object to INotifyPropertyChanged, and registered an handler for the event, which does get raised when the value of a property is changed.

UpdateMode of the databindings IS set to NotifyPropertyChanged, and CausesValidation is set to false on the associated control.

So pseudo-code looks like this :

class MyForm {
    MyClass localObj;
    BindingSource mySource = new BindingSource();

    MyForm(MyClass obj) {
        localObj = obj.CreateProxy() // Ensures localObj is a proxy instance
        mySource.Datasource = localObj // After this, UI is updated accordingly

        localObj.someProperty = "I changed the value" // Here, the NotifyPropertyChanged event does get fired, but the UI does not update. Hence my question...
        Console.WriteLine(localObj.someProperty) // Output is 'I changed the value'
    }

}

So the only way to get the UI to update is to call ResetBindings on the BindingSource… which kind of defeats the purpose of two way binding IMHO.

Finally !!

The problem comes from the Designer. At design-time, I don't have access to the real-type of the business object's proxy, which is a runtime type generated by the proxy (which is functionnal, I have no doubts about that part). So the BindingSource gets initialized by the designer using the known datatype of the business object, and not the datatype of the proxy (which is a class inheriting from the BO's class)

Hence, the datasource needs to be initialized manually with the runtime-type of the proxy object to make two-way binding work properly.

The working version is as follows (in pseudo C#) :

class MyForm {
    Object localObj;
    BindingSource mySource;

    MyForm(MyClass obj) {
        localObj = obj.CreateProxy() // Ensures localObj is a proxy instance

        ((ISupportInitialize) mySource).BeginInit()
        mySource.Datasource = localObj.GetType()
        ((ISupportInitialize) mySource).EndInit()

        mySource.Datasource = localObj // After this, UI is updated accordingly

        localObj.someProperty = "I changed the value" // UI updates ! :)
    }
}

Thank you to all for your advice and help, you just saved my day. I was banging my head against the wall on this one !

Best Answer

I would check if the dynamic code generation actually adds INotifyChanged before the form is loaded. Then I would check if the public property of the object that you change by code is actually the same object as the one that is currently displayed on the screen (is it a list or a single object?). Make sure the databinding is set to OnPropertyChanged instead of OnValidation. Then I would check if it works the other way around: edit the databound textbox-value and see if the object gets the new value after you select another control. Be carefull when you change the property with code like this (pseudocode).

ctor(MyClass objParm){
  this.localobj = objParm;
  bindingsource1.datasource = objParm; // or this.localobj
}
SomeMethod(){
  this.localobj = new MyClass();
  this.localobj.MyProperty = 'new value';
}

This will not work, except when you change to

SomeMethod(){
  this.localobj = new MyClass();
  this.localobj.MyProperty = 'new value';
  bindingsource1.datasource = this.localobj
}

Update:
You say that databinding does not work out of the box, but I can ensure you it does. After seeing the code your provided I suspect it has to do with the dynamic proxy generation / CreateProxy()-method. Since ResetBindings works, it probably has to do with the fact that the CreateProxy returns a different class then MyClass. Databinding is very picky concerning the types. E.g. if you use an interface, such as List for the datasource, Databinding can give errors because they are not the same types. That's why you need to use new BindingList() as a datasource. Just giving you some background information that might point you in the right direction. In your case, the metadata probably changes due to the dynamic code generation, have a look in the msdn documentation on ResetBindings(). As a test try this:

mySource.Datasource = new BindingList<MyClass>(new List<MyClass>(){this.localobj});
Related Topic