2013-08-02 24 views
2

我正在尋找一個屬性編輯器的一個例子是這樣一個屬性列表:PropertyGrid的編輯與繼承類

public class ContainerClass 
{ 
    public string ContainerName { get; set; } 
    public List<ContainerBase> Containers { get; set; } 

    public ContainerClasss() 
    { 
    Containers = new List<ContainerBase>(); 
    } 
} 

public class ContainerBase 
{ 
    public string Name { get; set; } 
    public string Description { get; set; } 
    public string Material { get; set; } 
    public float Area { get; set; } 
} 

public class Bookbag : ContainerBase 
{ 
    public int Pockets { get; set; } 

    public Bookbag() 
    { 
    Description = "Bookbag"; 
    } 
} 

public class Bookcase : ContainerBase 
{ 
    public Color Color { get; set; } 
    public int Shelves { get; set; } 

    public Bookcase() 
    { 
    Description = "Bookcase"; 
    } 
} 

在哪裏,當我點擊[...]按鈕容器中, [添加]按鈕可以讓我加入不同類型的容器,而不是容器基類的......

回答

3

您可以用自定義UITypeEditor屬性做到這一點:

public class ContainerClass 
{ 
    ... 
    [Editor(typeof(MyCollectionEditor), typeof(UITypeEditor))] 
    public List<ContainerBase> Containers { get; set; } 
    ... 
} 

有了這個UITy peEditor:

public sealed class MyCollectionEditor : CollectionEditor // need a reference to System.Design.dll 
{ 
    public MyCollectionEditor(Type type) 
     : base(type) 
    { 
    } 

    protected override Type[] CreateNewItemTypes() 
    { 
     return new[] { typeof(ContainerBase), typeof(Bookbag), typeof(Bookcase) }; 
    } 
} 

,這是它如何看:

enter image description here