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

编程入门 行业动态 更新时间:2024-10-24 19:14:51
本文介绍了如何避免在设计时在紧密绑定的用户控件中重置属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我有一个带有标签的 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屋!

更多推荐

[db:关键词]

本文发布于:2023-04-17 01:17:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/901366.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:绑定   控件   紧密   属性   用户

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!