问题
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;
}
}
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.
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?
回答1:
I had the same problem.
Try this:
[Category("Appearance")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public override string Text
{
get { return uxLabel.Text; }
set { uxLabel.Text = value; }
}
来源:https://stackoverflow.com/questions/1056080/how-can-i-avoid-properties-being-reset-at-design-time-in-tightly-bound-user-cont