Wednesday, February 11, 2009

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");
Submit this story to DotNetKicks

1 comment:

  1. very good solution, thank you.

    PS.

    Let people comment without signing in ;)

    ReplyDelete

Please try to keep comments relevant to the topic. All comments after 1 month will be moderated.