Expandable properties in .NET
Wed, 13 Jul 2011
How to spice up your toolbox behaviour
I had written a control with a number of properties, most of which showed up automatically in the Visual Studio Properties window. Simple data types were not a problem, nor were arrays: Visual Studio automatically opens up a window that lets the user edit the array elements. However, class or structure instances were not editable out of the box: they were shown grey-out in the Properties window.
The solution to this lies in that Visual Studio needs to know how the subclass or subproperty should be edited. An example of this is the Padding property that all controls share: it's a structure with a couple of properties (Left, Right, Bottom, Top and All), and in the Properties window you can expand the Padding property so you can edit any of these properties individually. This is precisely what I wanted for my substructures.
It's easy to convince Visual Studio .NET to give you this behavior by adding a TypeConverter attribute to your structure, like so:
public struct MyStruct
ExpandableObjectConverter is already implemented for you, as are some others. If you want different behavior in the Properties window, you can implement your own TypeConverter.