How can I avoid properties being reset at design-time in tightly bound user controls?

[亡魂溺海] 提交于 2019-12-12 18:20:34

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!