C# PropertyGrid-集合版本/包装器

C# PropertyGrid-集合版本/包装器,c#,winforms,propertygrid,C#,Winforms,Propertygrid,我想在PropertyGrid中编辑一种复杂的属性 interface IInterface{} abstract class Base : IInterface{} class A : Base{} class B : Base{} 这些类表示可以存储在属性中的内容(这些类的内容无关紧要) 第一个问题:我发现能够编辑对象的唯一解决方案是在CreateCollectionItemType()中提供一个具体的子类类型。为什么?如何避免这种情况 第二个问题:我现在想使用包装器将此属性赋予pr

我想在PropertyGrid中编辑一种复杂的属性

interface IInterface{}

abstract class Base : IInterface{}

class A : Base{}
class B : Base{}
这些类表示可以存储在属性中的内容(这些类的内容无关紧要)

  • 第一个问题:我发现能够编辑对象的唯一解决方案是在
    CreateCollectionItemType()
    中提供一个具体的子类类型。为什么?如何避免这种情况

  • 第二个问题:我现在想使用包装器将此属性赋予
    propertyGrid
    项。我不想在模型中包含属性属性(
    [Category(“General”)]
    ),而是想将它们放在包装器中

它适用于除系列之外的所有产品。 我是这样做的:

class abstract WrapperBase<T>
{
    T WrappedObject{get;set;}
}
class PropertyWrapper:WrapperBase<Property>
{
    List<Base> MyListOfObjects
    {
        get{return WrappedObject.MyListOfObjects;}
        set{WrappedObject.MyListOfObjects=value;}
    }
}
类抽象包装器库
{
T WrappedObject{get;set;}
}
类PropertyRapper:WrapperBase
{
列出MyListOfObjects
{
获取{return WrappedObject.MyListOfObjects;}
设置{WrappedObject.MyListOfObjects=value;}
}
}
这样,集合编辑器就不允许我将对象添加到此集合中,用于添加特定类型对象的下拉列表也就消失了

有什么想法吗?提前谢谢


[编辑]


问题的第二部分已经解决:因为包装器位于另一个程序集中,所以我没有在正确的位置查找IInterface的实现。

CreateNewItemTypes很好。在CreateCollectionItemType中,返回基类型。我认为这应该行。

我试过了,但是PropertyGrid没有提供特定于派生类型的字段。
class MyCollectionEditor : CollectionEditor
{
    public MyCollectionEditor(Type type) : base(type)
    {            
    }

    #region Overrides of CollectionEditor
    protected override Type[] CreateNewItemTypes()
    {
        base.CreateNewItemTypes();
        // [EDIT assembly, see below]
        var types = (from t in Assembly.GetAssembly(typeof(IInterface)).GetTypes()
                     where t.GetInterfaces().Contains(typeof (IInterface)) && !t.IsAbstract
                     select t).ToArray();
        return types;
    }

    protected override Type CreateCollectionItemType()
    {
        return typeof(A); // 1st problem
    }
}
class abstract WrapperBase<T>
{
    T WrappedObject{get;set;}
}
class PropertyWrapper:WrapperBase<Property>
{
    List<Base> MyListOfObjects
    {
        get{return WrappedObject.MyListOfObjects;}
        set{WrappedObject.MyListOfObjects=value;}
    }
}