[英]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.
只有重新啟動應用程序才會更改它。
在圖片中有一個PropertyBinding
到ApplicationSettings
的當前設置。
我已經嘗試過:
沒有什么不同。 有誰知道問題可能是什么?
問候,薩沙
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.