.NET control value not pushed to binding source

Peter Row

I have just upgraded an old .NET 1.1 WinForms app that uses the CSLA Framework, to .NET 4.0 and a newer version of CSLA. A large part of the reason for doing this was the data binding improvements, for example: being able to update binding on change instead of when validating (losing focus).

I have several custom user controls, one of which was a NumberBox. Essentially it’s just a textbox with a few properties such number type, decimal places etc. I exposed a public property of type object called BindableValue. It was to this property that I was binding my CSLA class’ (standard .NET classes inheriting validation rules, property changed and various stuff) integer property.

My problems having upgraded are the following:

  1. If I entered a value, e.g. 1234, into my number box control the binding didn’t push the value back into the class’ property until I tabbed off, even though I had configured an object binding source to the custom BindableValue property with the update mode set to Property change.
  2. Having entered a value, if I went back and deleted the value, I was prevented from tabbing off or clicking off the number box. I had set Visual Studio to throw when a .NET exception is thrown but it wasn’t breaking.

To fix these issues what I had to do in .NET 4.0 is actually much nicer than in .NET 1.1: Simply put, just like the class that I was binding my usercontrol to I had to implement the INotifyPropertyChanged interface. Then in my usercontrol when I wanted to pushback into my source object I just raised the PropertyChanged event (e.g. PropertyChanged("BindableValue"); ) the interface defined and hey presto all was well with the world again.

The INotifyPropertyChanged interface was introduced in .NET 2.0, so it should be possible to do the same in any version since then.