views:

475

answers:

4

Is there a simple way to implement databinding when neither of both classes is of type Control?

In my case, I would like to bind a variable to a property of a custom ToolStripButton.

EDIT for clarification: when binding to a Control, I can use Control's DataBindings collection. However, I am searching for a way to bind properties regardless of the source and target Type.

EDIT: using winforms

+1  A: 

Use dependency properties (your property in your ToolStripButton should be) and create a property for your variable in your other class and create a binding and set it to the property of your ToolstripButton.

I guess that's about the easiest way to do it.

EDIT: That's only for WPF...

Else implement INotifyPropertyChanged and when your variable changes, it should automatically change in your ToolStripButton.

Tony
Hmmm INotifyPropertyChanged seems to do this, but this would also mean that I'll have to take care of the synchronisation between those two myself, right? I.e. handle PropertyChanged event in both directions. If so, this would not really be like databinding, I think...
peter p
But hey... thanks for pointing me to this Interface. It is perfectly fine for another task :) Great.
peter p
+4  A: 

You can probably do this by using Truss.

Truss provides WPF-style databinding for any class that implements INotifyPropertyChanged. It gives you a bit more flexibility in this, since it doesn't restrict the classes to being derived from a specific base class.

Reed Copsey
Thanks for the link. I did not actually use Truss, however it pointed me to the right direction how I could roll a very copmact DataBinding class of my own :)
peter p
A: 

For similar behaviour like Controls being bound to object properties, for any Type you can implement the same interfaces.

Based on that thought, you can subclass ToolStripButton (or desired Type to have bindings) and implement IBindableComponent for it. This works for all kinds of source and target Types as long as they're not sealed. For example, your tool strip button:

public class BindableToolStripButton : ToolStripButton,  IBindableComponent {
    //...

This will cause the BindableToolStripButton to have its own .DataBindings property whereas the base ToolStripButton class doesn't have such a propery.

You would need to follow through on filling out implementation details using examples seen here from Microsoft for ISite, IBindableComponent, IComponent and any inherited interfaces.

Then you would add Binding instances to any instance of BindableToolStripButton.

(Note: I only have fragements so will make my first community wiki post - and we'll see how that goes... )

John K
Thank, your comment was very helpful. However, I decided not to use this technique, since there are quite some different classes which would have to implement these interfaces in my case. So I rolled my own, based on reflection and INotifyPropertyChanged (which is easier to implement than IBindableComponent).
peter p
A: 

I written some basic databinding stuff through reflection. It works on any object and doesn't need to implement something special (no INotifyPropertyChanged, it just works) it is part of my editor at http://github.com/filipkunc/opengl-editor-cocoa look at HotChocolate/Bindings (like re-implementation of Cocoa KVC, KVO into .NET) folder. You can see it in action in HotChocolateTest project.

Filip Kunc
Thank you, this looks great, too. Unfortunately I just saw your post when I had finished my own simple reflection solution. But I will definitely check this out if I need additional features, thanks!
peter p