[英]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.