You need to decorate your abstract base class Shape with the
TypeConverterAttribute
. For Example,
[TypeConverter(typeof(ExpandableObjectConverter))]
public abstract class Shape
{
public int Id { get; set; }
public string Name { get; set; }
public abstract double Area { get; }
}
Your derived classes can be implemented the regular way,
public abstract class Shape3D : Shape
{
public abstract double Volume { get; }
}
public class Cube : Shape3D
{
public double Height { get; set; }
public double Width { get; set; }
public double Depth { get; set; }
public override double Area
{
get
{
return 2*((Height*Width) + (Width*Depth) + (Depth*Height));
}
}
public override double Volume
{
get
{
return Height*Width*Depth;
}
}
}
ShapeContainer can contain a shape and can also have it's own properties,
public class ShapeContainer
{
public string ContainerProperty { get; set; }
public Shape ContainerShape { get; set; }
}
Now when an instance of the ShapeContainer is set to the property grid it will show the properties of the shape as well.
private void Form1_Load(object sender, EventArgs e)
{
Shape shape = new Cube() { Height = 2, Width = 3, Depth = 4, Id = 1, Name = "Cube1"};
ShapeContainer container = new ShapeContainer();
container.ContainerProperty = "ShapeContainer1";
container.ContainerShape = shape;
propertyGrid.SelectedObject = container;
}