属性网格和可扩展对象 [英] Property grid and Expandable objects

查看:64
本文介绍了属性网格和可扩展对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好!

我有一个问题,关于如何预览在PropertyGrid中具有某些属性的对象,但如何将这些属性组合在一个可扩展的类型属性中.我的模型与此类似:

我有一个名为"Shape"的抽象基类.

然后,我有几个抽象类派生了该类,例如"Shape2D"和"Shape3D".

最后,我有派生这两个类的类,例如"Circle","Sphere","Ellipse"等.

这些类中的每个类都有一些属性,例如半径",长度",侧面",高度"等.

我需要做的是能够在列表中选择"Shape"类时在PropertyGrid中显示这些属性.我应该做些什么修改才能做到这一点?我已经在网上进行了搜索,但是所有示例都不必要地复杂以显示这种行为.一个简单示例的链接也足够了!

提前谢谢!



对于更改问题我深表歉意,但我忘记了一件重要的事情.类"Shape"包含在另一个名为"ShapeContainer"的类中,该类具有自己的属性.现在,我需要在PropertyGrid中预览具有所有属性的"ShapeContainer"类,同时还要具有"Shape"属性(在可扩展属性中).

再次感谢!

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!



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!

推荐答案

您需要用TypeConverterAttribute装饰抽象基类Shape.例如,

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可以包含形状,也可以具有自己的属性,



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; }
}



现在,将ShapeContainer的实例设置为属性网格时,它将同时显示形状的属性.



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;
}


这篇关于属性网格和可扩展对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆