none
Bind Property in ToolStripMenuItem

    Question

  • Hi,
    I would to bind a ToolStripMenuItem property to a my Control property.
    ToolStripMenuItem is an item of a ToolStrip.
    I can find property DataBinding in ToolStripMenuItem to add Binding!!
    Usually I bind a property to another in the way I show you, in the example I bind the visibility of two combobox (supposing that two comboBox already exist)

     

     Me.combobox1.DataBindings.Add("Visible", me.combobox2, "Visible))

     

    But I can do in the same way for ToolStripMenuItem because DataBindings is not show, probably because ToolStripMenuItem not inheriths from System.Windsows.Form.Control.
    Thanks and Regards,
    Mauro


     

    Tuesday, January 22, 2008 11:03 AM

Answers

  •  

    Hi Mauro,

    Yes, you are right. Data binding is designed to bind a control to a data object. Non-control components that inherit from the Component don’t have the DataBindings property. If you want to implement the data binding for the component, you can implement the IBindableComponent for the non-control component. Try something like the following:

    Code Snippet

        class BindableToolStripMenuItem : ToolStripMenuItem,IBindableComponent

        {

            #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

        }

     

    Hope this helps.
    Best regards.
    Rong-Chun Zhang

    Thursday, January 24, 2008 7:47 AM

All replies

  •  

    Hi Mauro,

    Yes, you are right. Data binding is designed to bind a control to a data object. Non-control components that inherit from the Component don’t have the DataBindings property. If you want to implement the data binding for the component, you can implement the IBindableComponent for the non-control component. Try something like the following:

    Code Snippet

        class BindableToolStripMenuItem : ToolStripMenuItem,IBindableComponent

        {

            #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

        }

     

    Hope this helps.
    Best regards.
    Rong-Chun Zhang

    Thursday, January 24, 2008 7:47 AM
  • Hi Rong-Chung Zhang,
    so I had to implement IBindableComponent to make possible add Bindings.
    If you didn't say to me I was not able to write hard code like you.
    Thanks,
    Mauro

     

    Thursday, January 24, 2008 9:07 AM