ToolStripLabel 未使用 PropertyBinding 更新应用程序设置 [英] ToolStripLabel not updated with application settings using PropertyBinding

查看:27
本文介绍了ToolStripLabel 未使用 PropertyBinding 更新应用程序设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可能有一个非常简单的问题,但找不到解决方案.绑定到 ToolStripLabel 的属性有问题.一个标签绑定到 App.Config 中的 COM 端口值.

如果我为 System.Windows.Forms.Label 标签绑定属性,则通过更改 COM 端口来更新 Text-Property 可以正常工作.但是当标签是 IN ToolStrip (System.Windows.Forms.ToolStripLabel) 时,标签不会通过在运行时更改 COM 端口的值来更新.
仅在重新启动应用程序时才会更改.

图中有PropertyBindingApplicationSettings的当前设置.

我已经试过了:

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

没什么区别.有没有人知道可能是什么问题?

您好,莎莎

使用 System.ComponentModel;使用 System.Drawing;使用 System.Windows.Forms;使用 System.Windows.Forms.Design;[ToolStripItemDesignerAvailability(ToolStripItemDesignerAvailability.ToolStrip |ToolStripItemDesignerAvailability.StatusStrip),工具箱项(假)]公共类 ToolStripDataLabel : ToolStripLabel, IBindableComponent{私有绑定上下文 m_Context;私有 ControlBindingsCollection m_Bindings;公共 ToolStripDataLabel() { }公共工具条数据标签(字符串文本):基础(文本){}公共工具条数据标签(图像图像):基础(图像){}public ToolStripDataLabel(string text, Image image) : base(text, image) { }//如果需要,添加其他构造函数[可浏览(假)]公共绑定上下文绑定上下文{得到 {if (m_Context == null) m_Context = new BindingContext();返回 m_Context;}设置 =>m_Context = 值;}[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]公共控件绑定集合数据绑定{得到 {if (m_Bindings == null) m_Bindings = new ControlBindingsCollection(this);返回 m_Bindings;}}}

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

解决方案

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.

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

这篇关于ToolStripLabel 未使用 PropertyBinding 更新应用程序设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆