Text of UserControl dissappears in designer after compilation

心不动则不痛 提交于 2019-12-12 16:31:08

问题


I have the wierdest thing going on in my solution. I have a button, which I customly made. It inherits UserControl, and its text is represented as a Label inside that control.

Naturally, I wanted the button's text to be overridden to set the Label's text:

Either,

    /// <summary>
    /// Gets or sets the text that appears in the button
    /// </summary>
    [Category("Appearance"), Description("Gets or sets the text on the button.")]
    [Browsable(true)]
    public override string Text
    {
        get
        {
            return base.Text;
        }
        set
        {
            base.Text = value;
            labelButtonText.Text = value;
        }
    }

Or

    /// <summary>
    /// Gets or sets the text that appears in the button
    /// </summary>
    [Category("Appearance"), Description("Gets or sets the text on the button.")]
    [Browsable(true)]
    public override string Text
    {
        get
        {
            return labelButtonText.Text ;
        }
        set
        {
            labelButtonText.Text = value;
        }
    }

Regardless of the method, when I use that button in another UserControls/Forms, the text I explicitly put inside in the designer dissappears after compilation.

I checked in the "Button.designer.cs" file, and there is no assignment of the text to null nor empty for neither the UserControl nor the Label.

EDIT: Moreover, when I set the Text property in the designer, it does not set in the *.designer.cs file.

Thanks in advance.


回答1:


Yes, this is by design. The UserControl.Text property looks like this:

[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
[EditorBrowsable(EditorBrowsableState.Never)]
[Bindable(false)]
public override string Text
{
    get
    {
        return base.Text;
    }
    set
    {
        base.Text = value;
    }
}

You took care of the [Browsable] attribute but not the [DesignerSerializationVisibility]. Hidden is what makes the text disappear. Fix it by undo-ing all of the attributes:

    [Browsable(true)]
    [EditorBrowsable(EditorBrowsableState.Always)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    [Bindable(true)]
    public override string Text {
        // etc..
    }


来源:https://stackoverflow.com/questions/6483984/text-of-usercontrol-dissappears-in-designer-after-compilation

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