如何避免在设计时在紧密绑定的用户控件中重置属性? [英] How can I avoid properties being reset at design-time in tightly bound user controls?
问题描述
我有一个带有标签的UserControl'A',并且具有以下属性:
I have UserControl 'A' with a label, and this property:
/// <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;
}
}
然后,我在其上带有UserControl'A'的UserControl'B',然后在设计器中将Text属性设置为我的示例标签".然后,我有了MainForm,上面有UserControl'B'.
I then have UserControl 'B' which has UserControl 'A' on it, and I set the Text Property to "My Example Label" in the designer. Then, I have my MainForm, which has UserControl 'B' on it.
每次构建或运行时,UserControl'A'的Text属性都会重置为其默认值.我想这是因为自从我进行重建以来,它同时重建了UserControl'A'和'B',从而导致了问题.
Each time I do a build or run, the Text property of UserControl 'A' is reset to its default value. I suppose this is because since I am doing a rebuild, it rebuilds both UserControl 'A' and 'B', thus causing the problem.
当在应用程序中使用紧密绑定的控件和表单时,如何找到一种更好的设计模式方法来避免这种类型的行为?
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?
推荐答案
我遇到了同样的问题.
尝试一下:
[Category("Appearance")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public override string Text
{
get { return uxLabel.Text; }
set { uxLabel.Text = value; }
}
这篇关于如何避免在设计时在紧密绑定的用户控件中重置属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!