2

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

Jimi
  • 29,621
  • 8
  • 43
  • 61
Sasha
  • 23
  • 5
  • I'm not absolutely sure, but you can try to change Binding.DataSourceUpdateMode for your binding to OnPropertyChanged. It can be done via (PropertiBinding) item's editor. – Serg Jan 05 '21 at 10:44
  • @Serg Thanks for the answer! Unfortunately, the "Property Page" is not accessible in the Items Editor. – Sasha Jan 05 '21 at 11:18

1 Answers1

2

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 == null) m_Context = new BindingContext();
            return m_Context;
        }
        set => m_Context = value;
    }

    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    public ControlBindingsCollection DataBindings {
        get {
            if (m_Bindings == null) m_Bindings = new ControlBindingsCollection(this);
            return m_Bindings;
        }
    }
}
Jimi
  • 29,621
  • 8
  • 43
  • 61