如何避免在设计时在紧密绑定的用户控件中重置属性? [英] How can I avoid properties being reset at design-time in tightly bound user controls?

查看:34
本文介绍了如何避免在设计时在紧密绑定的用户控件中重置属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有标签的 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 'B',它上面有 UserControl 'A',我在设计器中将 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆