[英]How can I avoid properties being reset at design-time in tightly bound user controls?
I have UserControl 'A' with a label, and this property: 我有带标签的UserControl'A',并且具有以下属性:
/// <summary>
/// Gets or Sets the text of the control
/// </summary>
[
Browsable(true),
EditorBrowsable(EditorBrowsableState.Always),
Category("Appearance")
]
public override string Text {
get {
return uxLabel.Text;
}
set {
uxLabel.Text = value;
}
}
I then have UserControl 'B' which has UserControl 'A' on it, and I set the Text Property to "My Example Label" in the designer. 然后,我有UserControl'B',上面带有UserControl'A',然后在设计器中将Text属性设置为“我的示例标签”。 Then, I have my MainForm, which has UserControl 'B' on it.
然后,我有了MainForm,上面有UserControl'B'。
Each time I do a build or run, the Text property of UserControl 'A' is reset to its default value. 每次执行构建或运行时,UserControl'A'的Text属性都会重置为其默认值。 I suppose this is because since I am doing a rebuild, it rebuilds both UserControl 'A' and 'B', thus causing the problem.
我想这是因为自从我进行重建以来,它同时重建了UserControl'A'和'B',从而导致了问题。
How can I go about a better approach to design pattern to avoid this type of behavior when working with tightly bound controls and forms in a application? 在应用程序中使用紧密绑定的控件和表单时,如何寻求一种更好的设计模式方法来避免此类行为?
I had the same problem. 我有同样的问题。
Try this: 尝试这个:
[Category("Appearance")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public override string Text
{
get { return uxLabel.Text; }
set { uxLabel.Text = value; }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.