Decaying Code

Where code comes to die

About the author

Maxime Rouiller is a passionate .NET technology specialist, working for 7 years in large software development, advocating Agile and TDD. Aware of the latest technological trends, he intervenes as a specialist in the .NET Montréal usergroup and acts regularly as a speaker for Web Form programmers on the MVC platform.

Month List

Easily enable databindings on a ToolStripButton

I was developping an application lately and I needed to bind the "Enabled" property of a ToolStripButton to my Presenter. I failed to find any "DataSource" or "DataBindings" property. I then decided to make my own button without reinventing the wheel to enable this capability.

Here's this simple class:

using System.Windows.Forms;

namespace TestProject
{
        public class ToolStripBindableButton : ToolStripButton, IBindableComponent
        {
            private ControlBindingsCollection dataBindings;

            private BindingContext bindingContext;

            public ControlBindingsCollection DataBindings
            {
                get
                {
                        if(dataBindings == null) dataBindings = new ControlBindingsCollection(this);
                        return dataBindings;
                }
            }

            public BindingContext BindingContext
            {
                get
                {
                    if(bindingContext == null) bindingContext = new BindingContext();
                    return bindingContext;
                 }
                set { bindingContext = value; }
            }
        }
}
        

Once you include this simple class inside your project/solution... you can easily convert any ToolStripButton into our new ToolStripBindableButton.

And I solved my problem like this:

myBindableButton.DataBindings.Add("Enabled", myPresenter, "CanDoSomething");

Categories: c# | code snippet | controls
Permalink | Comments (0) | Post RSSRSS comment feed
Comments are closed