Search code examples
c#winformstoolstriptoolstripitem

ToolStripLabel not updated with application settings using PropertyBinding


I probably have a very simple problem, but could not find a solution. I have a problem with the property binding to a ToolStripLabel. A Label is bound to COM port value in App.Config.

If I bind the property for a System.Windows.Forms.Label label, the update of Text-Property by changing the COM Port works fine as it is supposed to. But when the label is IN ToolStrip (System.Windows.Forms.ToolStripLabel), the label is not updated by changing the value for COM Port at runtime.
It will be changed only by new start of Application.

In the picture there is a current settings of PropertyBinding to ApplicationSettings.

I've already tried:

  • Application.DoEvents()
  • toolStrip.Update()
  • toolStrip.Refresh()
  • toolStrip.Invalidate()

Nothing makes difference. Does anyone have an idea what the problem could be?

Greetings, Sasha

ApplicationSettings

Label Properties Settings

Example


Solution

  • The ToolStripLabel Component doesn't implement DataBindings, as the Label Control does (that's why you can see a Label Control update its Text when the current setting is changed). When you add PropertyBindings to the Text property through the Designer, the Text is just set to the Properties.Default setting selected (you can see that in the Designer.cs file).

    You can build your own ToolStripLabel that implements IBindableComponent, decorate it with ToolStripItemDesignerAvailability flags that allow the ToolStrip or StatusStrip to acknowledge the existence of this custom Component, so you can add it directly from the selection tool.

    Add a PropertyBinding to the Text property and now, when the Setting changes, the Text is updated. You can see in the Designer.cs file that a DataBinding has been added.

    Bindable ToolStripLabel

    using System.ComponentModel;
    using System.Drawing;
    using System.Windows.Forms;
    using System.Windows.Forms.Design;
    
    [ToolStripItemDesignerAvailability(
        ToolStripItemDesignerAvailability.ToolStrip | 
        ToolStripItemDesignerAvailability.StatusStrip),
     ToolboxItem(false)
    ]
    public class ToolStripDataLabel : ToolStripLabel, IBindableComponent
    {
        private BindingContext m_Context;
        private ControlBindingsCollection m_Bindings;
    
        public ToolStripDataLabel() { }
        public ToolStripDataLabel(string text) : base(text) { }
        public ToolStripDataLabel(Image image) : base(image) { }
        public ToolStripDataLabel(string text, Image image) : base(text, image) { }
    
        // Add other constructors, if needed
    
        [Browsable(false)]
        public BindingContext BindingContext {
            get {
                if (m_Context is null) m_Context = new BindingContext();
                return m_Context;
            }
            set => m_Context = value;
        }
    
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public ControlBindingsCollection DataBindings {
            get {
                if (m_Bindings is null) m_Bindings = new ControlBindingsCollection(this);
                return m_Bindings;
            }
        }
    }