Search code examples
c#winformsuser-controlswindows-forms-designerdesigner

User control design time property not showing in properties window


I have a UserControl with a custom property:

public Size TestSize { get; set; }

I would like this property to show up in the properties window during design time of the UserControl, and not only where the UserControl is used as an instance (e.g. when dragged/dropped on a form).

Example: In the designer of the UserControl one can set properties like "AllowDrop", "AutoScroll", "BackColor" etc. I would like my custom property to show up just the same way in the designer.

I have considered and tried adding attributes to the property like:

[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]

but that does not seem to be the way to go.

Any help is appreciated!


Solution

  • If you really want to show the property in the designer of UserControl1, then the property should belong to its base class. That's because of how designer works

    • Assuming the root element of the designer is UserControl1, then the properties that you see in properties window, are browsable properties of UserControl1's base class (which is probably a UserControl). To learn more about it, read the second section of the answer.

    In most cases you don't need to show those properties in designer, unless you want to derive multiple controls from that base and configure that property in design-time of the derived control, but I assume you know your requirement and now you are able to make a decision base on your requiremets.


    This is the way that designer works:

    1. It deserialize the .cs file (and looks for InitializeComponent method, or designer.cs file, also considers the designer-related attributes).
    2. It creates an instance of the base class and add it to the design surface, and applies the deserialized property values. (So the instance that you see in designer, is an instance of the base class. It's a trick. For example, that's why if you have an abstract base class, you cannot have the derived class as root designer without some workarounds.)
    3. It creates the whole control/component trees based on the deserialized code. (So the controls which are on design surface are real instances of the controls)

    Of course there are a lot of other things happening, like making the extender providers working, or pre-filtering or post-filtering properties in design-time, but in general it works like what I explained above.

    More information / Related Posts

    Some other related answers: