C # how to implement data binding without control?

Is there an easy way to implement data binding when none 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 the Control DataBindings collection. However, I am looking for a way to bind properties independently of the source and target types.

EDIT: using winforms

+6
c # data-binding winforms
source share
6 answers

Perhaps you can do this using Truss .

Truss provides WPF-style data binding for any class that implements INotifyPropertyChanged. This gives you a little more flexibility in this, since it does not limit classes to derived from a specific base class.

+8
source share

I use this IBindableComponent Proof on the ToolStripButton found here . BindableToolStripButton allows you to use data binding, as with regular Control .

 [ToolStripItemDesignerAvailability(ToolStripItemDesignerAvailability.ToolStrip | ToolStripItemDesignerAvailability.StatusStrip)] public class BindableToolStripButton : ToolStripButton, IBindableComponent { public BindableToolStripButton() : base() { } public BindableToolStripButton(String text) : base(text) { } public BindableToolStripButton(System.Drawing.Image image) : base(image) { } public BindableToolStripButton(String text, System.Drawing.Image image) : base(text, image) { } public BindableToolStripButton(String text, System.Drawing.Image image, EventHandler onClick) : base(text, image, onClick) { } public BindableToolStripButton(String text, System.Drawing.Image image, EventHandler onClick, String name) : base(text, image, onClick, name) { } #region IBindableComponent Members private BindingContext bindingContext; private ControlBindingsCollection dataBindings; [Browsable(false)] public BindingContext BindingContext { get { if (bindingContext == null) { bindingContext = new BindingContext(); } return bindingContext; } set { bindingContext = value; } } [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)] public ControlBindingsCollection DataBindings { get { if (dataBindings == null) { dataBindings = new ControlBindingsCollection(this); } return dataBindings; } } #endregion } 

Assuming you have a MyClass class that implements INotifyPropertyChanged , use it the same way as when binding to the control property:

 bindableToolStripButton1.DataBindings.Add("Enabled", myClass1, "MyBooleanProperty"); 
+5
source share

Use the 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 think this is the easiest way to do this.

EDIT: This is only for WPF ...

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

+1
source share

For similar behavior, such as controls related to object properties, for any type, you can implement the same interfaces.

Based on this thought, you can subclass ToolStripButton ( or the desired type for the binding ) and implement an IBindableComponent for it. This works for all source types and target types if they are not sealed . For example, a button on a toolbar:

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

This will cause BindableToolStripButton to have its own .DataBindings property, while the base class ToolStripButton does not have such a property.

You will need to fill out implementation details using examples from Microsoft for ISite , IBindableComponent , IComponent, and any legacy interfaces.

You then added Instance Linking to any instance of BindableToolStripButton.

(Note: I only have snippets, so I will make my first community wiki forum and we will see how it works ...)

+1
source share

I wrote some basic data to bind data through reflection. It works on any object and does not need to implement anything special (without INotifyPropertyChanged, it just works), it is part of my editor at http://github.com/filipkunc/opengl-editor-cocoa see HotChocolate / Bindings (for example , re-implementation of the Cocoa KVC, KVO folder in .NET). You can see it in action in the HotChocolateTest project.

0
source share

There is another quick and easy solution, which consists in creating properties in the form and binding them:

 public MyForm : Form { ... public bool CanDelete { get { return deleteToolStripButton.Enabled; } set { deleteToolStripButton.Enabled = value; } } public MyForm() { ... this.DataBindings.Add("CanDelete", this.MyModel, "DeleteAllowed", false, DataSourceUpdateMode.Never); ... } } 

Assuming MyModel contains a DeleteAllowed property that notifies you of changes.

0
source share

All Articles