I need to bind a property to a ToolStripMenuItem. I've searched around and found it imposible, the best workaround seems to be creating a BindableToolStripMenuItem class and implement it yourself. So I've taken some reasonabyl well established code from the internet:

    public partial class BindableToolStripMenuItem : ToolStripMenuItem,IBindableComponent
    {
        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;
            }
        }
    }

Now all I need is to bind it right? I'm trying to bind it to the IsHidden property of a window (actually a DockContent form the DockPanelSuite).

I tried:

meterEditorToolStripMenuItem.DataBindings.Add(new Binding("Checked", MeterEditor, "IsHidden"));

It works fine on initialisation, but when I change the hidden state, there's no update. After a bit of looking around, I tried this:

meterEditorToolStripMenuItem.DataBindings.Add(new Binding("Checked", MeterEditor, "IsHidden", true, DataSourceUpdateMode.OnPropertyChanged));

Still no luck. I tried to add a NotifyPropertyChanged to the property setter, but all I did was 'mask' the actual property

public new Boolean IsHidden {
    get {
        return base.IsHidden;
    }
    set {
        NotifyPropertyChanged("IsHidden");
        base.IsHidden = value;
    }
}

public event PropertyChangedEventHandler PropertyChanged;

private void NotifyPropertyChanged(String info) {
    if (PropertyChanged != null) {
        PropertyChanged(this, new PropertyChangedEventArgs(info));
    }
}

Is there a way to override the existing property, or the setter? Anything else I should try?

Really frustrating. Every reference I can find on the internet relating to IBindableControl looks exactly like this. I can't find anyone with the same problem.

Anyone have any tips on debugging bindings that just... don't bind? Forgetting about the INotify - simply checking the control doesn't even trigger anything to do with the binding...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.