【问题标题】:PropertyGrid Editor for a property list with inherited classes具有继承类的属性列表的 PropertyGrid 编辑器
【发布时间】:2013-08-03 18:01:28
【问题描述】:

我正在寻找类似以下的属性编辑器示例:

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

当我单击 Containers 的 [...] 按钮时,[ADD] 按钮允许我添加不同类型的容器,而不是基础容器类...

【问题讨论】:

    标签: c# propertygrid propertyeditor


    【解决方案1】:

    您可以使用自定义 UITypeEditor 属性来做到这一点:

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

    有了这个 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) };
        }
    }
    

    这就是它的外观:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-01-30
      • 2011-10-08
      • 1970-01-01
      • 1970-01-01
      • 2023-01-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多