gpt4 book ai didi

c# - 具有继承类的属性列表的 PropertyGrid 编辑器

转载 作者:行者123 更新时间:2023-11-30 15:33:03 24 4
gpt4 key购买 nike

我正在寻找属性编辑器的示例,例如:

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] 按钮允许我添加不同类型的容器,而不是基础容器类...

最佳答案

您可以使用自定义的 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) };
}
}

这就是它的样子:

enter image description here

关于c# - 具有继承类的属性列表的 PropertyGrid 编辑器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18017349/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com