I have yet to find a ‘nice’ way to do two way databinding in .Net.
The one thing I don’t like for example in the present asp.net two way databinding is doing the binding in the aspx page is no compile time checking, ie:
<asp:TextBox ID='TitleTextBox' runat='server' Text='<%# Bind('Title_oops_spelled_wrong') %>'>
I would like to have something like this:
Class Binder Public Sub BindControl(ctl As Control, objectProperty As ???????) '// Add ctl and objectProperty to a collection End Sub
What I don’t know is possible is, how to receive the objectProperty in my example; I want to receive a reference (ie: a pointer) to the property, so later, via this reference, I can either read from or write to the property.
Can this somehow be done with delegates perhaps??
UPDATE:
Note, I want to add the control reference to a binding collection, this collection would then be used for binding and unbinding.
Is there a reason that you want to avoid using the .NET data binding mechanism? It handles keeping parity between the control’s value and the class’s property value already, and provides rich design-time support.
You can interface with the data binding engine in two ways: either programatically or through the designer. To do basic data binding programatically is trivial. Say I have a class named ‘FooClass’ with a string property named ‘MyString’. I have a TextBox named myTxtBox on a form with an instance of FooClass called foo and I want to bind it to the MyString property:
Executing this will cause updates to the TextBox to get assigned to the property, and changes to the property from elsewhere will be reflected in the TextBox.
For more complex data binding scenarios, you’ll probably want to create an Object Data Source in your project and put a BindingSource on your form. If you need help with specific steps in creating the data source I can help, but in general you’ll create the source in your project and select the class to which you want to bind. You can then place a BindingSource on your form, point it at your object project data source, then use the Visual Studio designer to bind properties for your controls to properties on your object. You then set the DataSource property in your code to an instance of the class or collection to which you want to bind.
As a side note, as far as I am aware there is no ‘property delegate’, as properties are actually function pairs (a get_ and a set_).
UPDATE:
As I read your comments, I’d like to point out that .NET data binding, even at the control level, does NOT automatically use reflection. Data binding is built around type descriptors and property descriptors, both for the bound control and the data source. It’s true that if one or both of these sides does not implement specific property and type description then reflection will be used, but either side is more than free to provide its own description profile which would NOT use reflection.