tags:

views:

999

answers:

2

I used an application recently that was awe-inspiring. All the forms inherited from their own classes and a 'form' came with an amazing amount of functionality and looked the nuts.

What I'm interested in is this 'feature' of the form. This was a C# WinForms project and blew me away.

The forms were bound to objects that the group had written to support two-way data binding (to an extent). The way they behaved was pretty simple:

The data input forms all had controls that inherited from textbox and these were bound to properties of an object, Entering data immediately validated it and the box was a light pink if validation failed and a light green if it passed. If the box ever turns blue this actually means the value in the database the form is bound to has changed and your changes to the controls were immediately saved when valid values entered. It was the case that sometimes a section of controls had to be filled before a save occured. But it was all automatic. You could stop at any point and come back later and continue without actually saving yourself.

And like I say if someone else is editing the same record values they change caused your textboxes to become blue and you knew you needed to reload the screen to see up to date information.

All of this came from using their own form class they had written and their own textbox controls bound to an objects property.

I'm mainly wondering how the hell did the object figure out the value had been changed by someone else. It surely isn't polling the database. This system was amazing. The brilliance didn't stop there.

For simplicity. How might I create an object or collection of objects to mimic the bahaviour. I'm not going to but I can't even see how.

Thanks

+1  A: 

I'm pretty sure that anything involving other people's changes would have needed to hit the database. For two-way binding, all you really need is change notification - i.e. INotifyPropertyChanged (or a FooChanged event for every Foo property). This is all abstracted into TypeDescriptor - i.e. any binding that uses the regular PropertyDescriptor implementation (which it should) will know about notifications via SupportsChangeEvents, AddValueChanged and RemoveValueChanged.

For validation - IDataErrorInfo is your friend; by implementing this, you can volunteer validation information (which is used and displayed by several controls, such as DataGridView). i.e.

IDataErrorInfo dei = obj as IDataErrorInfo;
if(dei != null) { // supports validation
   string err = dei["PropName"]; // or .Error for overall status
   bool clean = string.IsNullOrEmpty(err);
}

Note that an alternative approach would be to have a Color property on the data aobject, and bind that directly to the textbox etc.

Marc Gravell
A: 

Ok thanks i'll look at some of the terms you mentioned :). There's a few new keywords in there for me.

Thanks

Robert
Let me know if you struggle with anything; I can knock a demo together easily enough...
Marc Gravell