C# ToolStripLabel未使用PropertyBinding使用应用程序设置更新
我可能有一个非常简单的问题,但找不到解决方案。我对绑定到ToolStripLabel的属性有问题。标签绑定到App.Config中的COM端口值 如果我绑定了C# ToolStripLabel未使用PropertyBinding使用应用程序设置更新,c#,winforms,toolstrip,toolstripitem,C#,Winforms,Toolstrip,Toolstripitem,我可能有一个非常简单的问题,但找不到解决方案。我对绑定到ToolStripLabel的属性有问题。标签绑定到App.Config中的COM端口值 如果我绑定了System.Windows.Forms.LabelLabel的属性,那么通过更改COM端口来更新Text属性就可以正常工作。但当标签位于ToolStrip(System.Windows.Forms.ToolStripLabel)中时,不会在运行时通过更改COM端口的值来更新标签。 只有在新的应用程序开始时,才会对其进行更改 在图片中,当前
System.Windows.Forms.Label
Label的属性,那么通过更改COM端口来更新Text属性就可以正常工作。但当标签位于ToolStrip(System.Windows.Forms.ToolStripLabel
)中时,不会在运行时通过更改COM端口的值来更新标签。只有在新的应用程序开始时,才会对其进行更改 在图片中,当前设置为
PropertyBinding
到ApplicationSettings
我已经试过了:
- Application.DoEvents()
- toolStrip.Update()
- toolStrip.Refresh()
- toolStrip.Invalidate()
PropertyBindings
添加到Text
属性时,文本仅设置为Properties。默认设置为选中(您可以在Designer.cs
文件中看到)
您可以构建自己实现的ToolStripLabel,使用允许ToolStrip或StatusStrip确认此自定义组件存在的标志对其进行装饰,以便您可以直接从选择工具添加它
将PropertyBinding
添加到文本属性,现在,当设置更改时,文本将更新。您可以在Designer.cs
文件中看到添加了数据绑定
我不是绝对确定,但您可以尝试更改绑定到OnPropertyChanged的Binding.DataSourceUpdateMode。可以通过(PropertyBinding)项的编辑器来完成。@Serg感谢您的回答!不幸的是,“属性页”在项目编辑器中不可访问。
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, e.g.
public ToolStripDataLabel(string text, Image image, bool isLink) : base(text, image, isLink, null) { }
[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;
}
}
}