Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# how to implement databinding without Control?

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

like image 453
peter p Avatar asked Jan 04 '10 20:01

peter p


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


2 Answers

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.

like image 130
Reed Copsey Avatar answered Nov 03 '22 22:11

Reed Copsey


I use this Implemetation of IBindableComponent on the ToolStripButton, found here. The BindableToolStripButton allows you to use Databinding like with a normal 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 class MyClass implementing INotifyPropertyChanged, use it just like you would when binding to a control property:

bindableToolStripButton1.DataBindings.Add("Enabled", myClass1, "MyBooleanProperty");
like image 26
richn Avatar answered Nov 03 '22 23:11

richn