Oztaco
Oztaco

Reputation: 3459

Why won't my Custom Control's Text property show up in the Properties window?

I have a user control that inherits from UserControl. It's a button so I'm trying to make the text in the button, change-able by using the Text property like the real buttons, instead of naming my own like _Text. I have the following code but it doesn't work (ie it doesn't show up in the Property Window). The name of the label is ContentPresenter

public override string Text
{
    get
    {
        return ContentPresenter.Text;
    }
    set
    {
        ContentPresenter.Text = value;
    }
}

Upvotes: 9

Views: 6695

Answers (1)

Igby Largeman
Igby Largeman

Reputation: 16747

UserControl goes to significant effort to hide the Text property. From the metadata:

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

You can make it visible by overriding those attributes in your code:

    [Browsable(true)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    [EditorBrowsable(EditorBrowsableState.Always)]
    [Bindable(true)]
    public override string Text 
    { 
        get { return ContentPresenter.Text; } 
        set { ContentPresenter.Text = value; } 
    } 

I'm not promising that's enough to make it work, but it probably is.

Upvotes: 17

Related Questions