简体   繁体   English

ToolStripLabel 未使用 PropertyBinding 更新应用程序设置

[英]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.我对 ToolStripLabel 的属性绑定有疑问。 A Label is bound to COM port value in App.Config. Label 绑定到 App.Config 中的 COM 端口值。

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. 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. 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 .在图片中有一个PropertyBindingApplicationSettings的当前设置。

I've already tried:我已经尝试过:

  • Application.DoEvents()应用程序.DoEvents()
  • toolStrip.Update()工具条更新()
  • toolStrip.Refresh() toolStrip.Refresh()
  • toolStrip.Invalidate() toolStrip.Invalidate()

Nothing makes difference.没有什么不同。 Does anyone have an idea what the problem could be?有谁知道问题可能是什么?

Greetings, Sasha问候,萨沙

ApplicationSettings应用程序设置

Label Properties Settings Label 属性设置

Example例子

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). ToolStripLabel组件没有像 Label 控件那样实现 DataBindings(这就是为什么您可以看到 Label 控件在当前设置更改时更新其文本的原因)。 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).当您通过 Designer 将PropertyBindings添加到Text属性时,Text 只是设置为选中的Properties.Default设置(您可以在Designer.cs文件中看到)。

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.您可以构建自己的实现IBindableComponent的 ToolStripLabel ,并使用允许 ToolStrip 或 StatusStrip 确认此自定义组件存在的ToolStripItemDesignerAvailability标志来装饰它,因此您可以直接从选择工具中添加它。

Add a PropertyBinding to the Text property and now, when the Setting changes, the Text is updated.PropertyBinding添加到 Text 属性,现在,当 Setting 更改时,Text 会更新。 You can see in the Designer.cs file that a DataBinding has been added.您可以在Designer.cs文件中看到已添加 DataBinding。

可绑定工具条标签

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;
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM