如何在紧密绑定的用户控件中避免在设计时重置属性?

Dav*_*son 2 c# architecture user-controls design-time

我有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;
        }
    }
Run Code Online (Sandbox Code Playgroud)

然后我有UserControl'B',其上有UserControl'A',我在设计器中将Text Property设置为"My Example Label".然后,我有我的MainForm,它上面有UserControl'B'.

每次进行构建或运行时,UserControl的"A"的Text属性都会重置为其默认值.我想这是因为我正在进行重建,它会重建UserControl'A'和'B',从而导致问题.

在应用程序中使用紧密绑定的控件和表单时,如何更好地设计模式以避免此类行为?

小智 6

我有同样的问题.

试试这个:

[Category("Appearance")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public override string Text
{
     get { return uxLabel.Text; }
     set { uxLabel.Text = value; }
}
Run Code Online (Sandbox Code Playgroud)