R – Data Binding using INotifyPropertyChanged (.Net)

data-bindinginotifypropertychangedmvp

I'm trying to find a good way to implement MVP in classic Winforms, and a couple of solutions I've come accros (e.g. http://codebetter.com/blogs/jeremy.miller/archive/2007/05/25/build-you-own-cab-part-3-the-supervising-controller-pattern.aspx) talk about using data binding between the model and the view. I've never used data binding before, so I thought I'd give it a try.

The trouble is, I can't find out how to do simple binding (e.g. a string in my model class to a textbox on the form) using INotifyPropertyChanged, as suggested in the above article. I thought I had it worked out (this is in the form, where 'model' is an instance of my model class):

txtModelName.DataBindings.Add(new Binding("Text", model, "Name"));

However, I soon realised that this didn't use INotifyPropertyChanged at all – it works fine whether I implement that interface on my model or not. Not a problem in itself, but it doesn't work the way I want it to, the main problem being that it's 2-way binding (I only want to bind from the object to the form).

I'm assuming that either there's a different way of binding using INotifyPropertyChanged, or that binding done as above can be set to only work in one direction – can anyone help out here, or point me towards a decent example?

I'm using .Net 3.5 with classic winforms, not WPF.

Thanks

Best Answer

As it turns out, the situation above was a bit of a red herring. What was happening was that I was binding from a textbox to the model, and from the model to another textbox. If I updated the model directly, it wouldn't work without implementing INotifyPropertyChanged.

As for the 2-way binding, I think you just have to live with that if you're using this method. If you don't want that, just bite the bullet and use the 'Passive View' style of MVP.