簡體   English   中英

ToolStripLabel 未使用 PropertyBinding 更新應用程序設置

[英]ToolStripLabel not updated with application settings using PropertyBinding

我可能有一個非常簡單的問題,但找不到解決方案。 我對 ToolStripLabel 的屬性綁定有疑問。 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. 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.
只有重新啟動應用程序才會更改它。

在圖片中有一個PropertyBindingApplicationSettings的當前設置。

我已經嘗試過:

  • 應用程序.DoEvents()
  • 工具條更新()
  • toolStrip.Refresh()
  • toolStrip.Invalidate()

沒有什么不同。 有誰知道問題可能是什么?

問候,薩沙

應用程序設置

Label 屬性設置

例子

ToolStripLabel組件沒有像 Label 控件那樣實現 DataBindings(這就是為什么您可以看到 Label 控件在當前設置更改時更新其文本的原因)。 當您通過 Designer 將PropertyBindings添加到Text屬性時,Text 只是設置為選中的Properties.Default設置(您可以在Designer.cs文件中看到)。

您可以構建自己的實現IBindableComponent的 ToolStripLabel ,並使用允許 ToolStrip 或 StatusStrip 確認此自定義組件存在的ToolStripItemDesignerAvailability標志來裝飾它,因此您可以直接從選擇工具中添加它。

PropertyBinding添加到 Text 屬性,現在,當 Setting 更改時,Text 會更新。 您可以在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