sanger
sanger

Reputation: 63

.NET Custom UserControl with Structured/Multi-level Properties

I can create a Custom UserControl with simple properties (Integer, String, etc) and have those properties show up in the Property Panel. I can also create a Custom UserControl with properties like Size, Rectangle, etc and the properties will show up as an expandable item in the Property Panel (click on the '+' and the Size expands to Width & Height).

Is it possible to create properties with my own custom structure? e.g. Property 'Message' expands to Text, ForeColor, BackColor, Blink etc. I have tried creating a property that references a simple class or structure with containing the properties representing my custom structure but in the Property Panel the property is greyed out and cannot be expanded or modified.

Upvotes: 1

Views: 1050

Answers (1)

Igby Largeman
Igby Largeman

Reputation: 16747

You need to provider a TypeConverter for your custom type so that it can be converted to and from a string, and then decorate your custom type with the TypeConverterAttribute.

Derive your TypeConverter from ExpandableObjectConverter.

public class MyTypeConverter : ExpandableObjectConverter
{
}

Override CanConvertTo(), ConvertTo(), CanConvertFrom(), and ConvertFrom() to provide the ability to convert the custom type to a string (this is the value that appears in the property grid on the main row which you can see before expanding) and from a string back to itself. A common string representation would be to show a list of all the field values separated by commas.

Decorate the custom type with the TypeConverterAttribute.

[TypeConverter(typeof(MyTypeConverter ))]
public struct MyType
{
}

That's the bare minimum to get you started. There is considerably more to learn. This MDSN article might be a good place to start.

Upvotes: 1

Related Questions