Click here to Skip to main content
15,884,628 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello!

I have a question on how to preview an object that has certain properties in the PropertyGrid but in such a way that the properties are grouped in an expandable type property. My model is similar to this:

I have an abstract base class called "Shape"

Then, I have several abstract classes that derive that class like "Shape2D" and "Shape3D".

Finally, I have classes that derive those two classes, like "Circle", "Sphere", "Ellipse" and so on.

Every one of these classes have some properties like "Radius", "Length", "Side", "Height" and such.

What I would need to do is to be able to show those properties in a PropertyGrid when a "Shape" class is selected in a list. What modifications should I do to make this possible? I've searched all over the net, but all the examples are unnecessarily complicated to show this kind of behavior. A link to a simple example would also suffice!!

Thank you in advance!

EDIT:

I apologize for changing the question, but I forgot one important thing. The class "Shape" is contained in another class called "ShapeContainer" that has properties of it's own. Now, I would need to preview the "ShapeContainer" class in the PropertyGrid with all it's properties, but with the "Shape" properties as well (within an expandable property).

Thanks again!
Posted
Updated 19-Apr-10 4:00am
v2

1 solution

You need to decorate your abstract base class Shape with the TypeConverterAttribute. For Example,

C#
[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,

C#
public abstract class Shape3D : Shape
{
    public abstract double Volume { get; }
}


C#
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,

C#
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.

C#
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;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900