Question

I'm looking for an example of a property editor for something like:

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

Where when I click on the [...] button for Containers, the [ADD] button allows me to add the different types of containers, not the base container class...

Was it helpful?

Solution

You can do it with a custom UITypeEditor attribute:

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

With this UITypeEditor:

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

And this is how it will look:

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top