Search code examples
.netinheritancecollectionselementpropertygrid

Is there anyway to make PropertyGrid's CollectionEditor to be able to add into collection inherited classes?


enum ClassType
{
    BASE,
    A,
    B
}

class BaseClass
{
    ClassType CT;
    //some fields
}

class AClass : BaseClass
{
    //some fields
}

class BClass : BaseClass
{
    //some fields
}

class Holder
{
    public List<BaseClass> list { get; set; }
}

Then I have a collection List<BaseClass> list;

If I load Holder's instance as PropertyGrid.SelectedObject, I will be able to add, edit and remove elements in collection, but I could add ONLY BaseClass and what I want is the choice of what class to add in the list.

Is this possible without making my own collection editor?

Need something like that for ContextMenuStrip.Items editor, but I don't know how it's made. enter image description here


Solution

  • You might want to try inheriting from it and overriding the NewItemTypes property:

    http://msdn.microsoft.com/en-us/library/system.componentmodel.design.collectioneditor.newitemtypes(v=vs.110).aspx

    You will need to add an EditorAttribute to your collection type to specify your inherited editor.