Preventing the designer setting properties of a User Control

July 4th, 2007

I’ve built a couple of user controls to simplify some data capture in our current application, one of which is a date choosing control. It exposes a couple of fields exposed as properties that should only be used by code and not accessible through the design-time property grid. I quickly discovered the decoration [Browsable(false)] to stop the property from showing up in the Properties grid, but the designer was still setting them in InitializeComponent; until I discovered the power of [DesignerSerializationVisibility].
Decorating the property with [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] supresses the serialization of the value into the designer code. There are two other options - Visible which only serializes the value if it isn’t the default (and you can use the DefaultValue to set that) and Content which does something clever with collection properties.
Aren’t attributes great!

Posted in Development |

Comments

Leave a Reply

You must be logged in to post a comment.