C# WPF Toolkit Community Edition属性网格:集合编辑器中的新项类型

C# WPF Toolkit Community Edition属性网格:集合编辑器中的新项类型,c#,wpf,collections,wpftoolkit,propertygrid,C#,Wpf,Collections,Wpftoolkit,Propertygrid,我使用WPF Toolkit CE的属性网格。 将对象添加到集合时,打开的对话框将显示一个组合框,用于选择要添加的新类型。在我阅读的NewItemTypesAttribute文档中: 此属性可以修饰所选对象的集合属性(即IList),以便控制允许在CollectionControl中实例化的类型 但是我不能让它工作。 以下是我尝试的最后一个变体: namespace TestPropertyGrid { /// <summary> /// Interaktionslogik

我使用WPF Toolkit CE的属性网格。 将对象添加到集合时,打开的对话框将显示一个组合框,用于选择要添加的新类型。在我阅读的
NewItemTypesAttribute
文档中: 此属性可以修饰所选对象的集合属性(即IList),以便控制允许在CollectionControl中实例化的类型

但是我不能让它工作。 以下是我尝试的最后一个变体:

namespace TestPropertyGrid
{
  /// <summary>
  /// Interaktionslogik für MainWindow.xaml
  /// </summary>
  public partial class MainWindow : Window
  {
    public MainWindow()
    {
      InitializeComponent();

      MyObject myObj = new MyObject();
      this.PGrid.SelectedObject = new MyObject();
    }
  }

  public class MyObject
  {
    [NewItemTypes(typeof(MyBaseObj), typeof(MyObj1))]
    [Editor(typeof(CollectionEditor), typeof(CollectionEditor))]
    public ArrayList ListOfObjects { get; set; } = new ArrayList();
  }

  public class MyBaseObj
  {
  }

  public class MyObj1
  {
  }
}

我希望我能清楚地解释我的问题,有人能帮助我。谢谢。

奇怪。。。看起来,这并不是100%实现的。 现在我制作了自己的CollectionEditor,并自己设置了该属性的类型

class MyCollectionEditor : TypeEditor<CollectionControlButton>
{
  protected override void SetValueDependencyProperty()
  {
    ValueProperty = CollectionControlButton.ItemsSourceProperty;
  }

  protected override void ResolveValueBinding(PropertyItem propertyItem)
  {
    var type = propertyItem.PropertyType;
    Editor.ItemsSourceType = type;
    // added
    AttributeCollection attrs = propertyItem.PropertyDescriptor.Attributes;
    Boolean attrFound = false;
    foreach(Attribute attr in attrs)
    {
      if (attr is NewItemTypesAttribute)
      {
        Editor.NewItemTypes = ((NewItemTypesAttribute)attr).Types;
        attrFound = true;
        break;
      }
    }
    // end added
    if (!attrFound)
    {
      if (type.BaseType == typeof(System.Array))
      {
        Editor.NewItemTypes = new List<Type>() { type.GetElementType() };
      }
      else if (type.GetGenericArguments().Count() > 0)
      {
        Editor.NewItemTypes = new List<Type>() { type.GetGenericArguments()[0] };
      }
    }
    base.ResolveValueBinding(propertyItem);
  }
}
类MyCollectionEditor:TypeEditor
{
受保护的覆盖无效SetValueDependencyProperty()
{
ValueProperty=CollectionControlButton.ItemsSourceProperty;
}
受保护的覆盖无效ResolveValueBinding(PropertyItem PropertyItem)
{
变量类型=propertyItem.PropertyType;
Editor.ItemsSourceType=类型;
//增加
AttributeCollection attrs=propertyItem.PropertyDescriptor.Attributes;
布尔属性=false;
foreach(attrs中的属性attr)
{
if(attr是NewItemTypesAttribute)
{
Editor.NewItemTypes=((NewItemTypesAttribute)attr).Types;
attrFound=true;
打破
}
}
//结束添加
如果(!找到)
{
if(type.BaseType==typeof(System.Array))
{
Editor.NewItemTypes=新列表(){type.GetElementType()};
}
else if(type.GetGenericArguments().Count()>0)
{
Editor.NewItemTypes=new List(){type.GetGenericArguments()[0]};
}
}
base.ResolveValueBinding(propertyItem);
}
}
class MyCollectionEditor : TypeEditor<CollectionControlButton>
{
  protected override void SetValueDependencyProperty()
  {
    ValueProperty = CollectionControlButton.ItemsSourceProperty;
  }

  protected override void ResolveValueBinding(PropertyItem propertyItem)
  {
    var type = propertyItem.PropertyType;
    Editor.ItemsSourceType = type;
    // added
    AttributeCollection attrs = propertyItem.PropertyDescriptor.Attributes;
    Boolean attrFound = false;
    foreach(Attribute attr in attrs)
    {
      if (attr is NewItemTypesAttribute)
      {
        Editor.NewItemTypes = ((NewItemTypesAttribute)attr).Types;
        attrFound = true;
        break;
      }
    }
    // end added
    if (!attrFound)
    {
      if (type.BaseType == typeof(System.Array))
      {
        Editor.NewItemTypes = new List<Type>() { type.GetElementType() };
      }
      else if (type.GetGenericArguments().Count() > 0)
      {
        Editor.NewItemTypes = new List<Type>() { type.GetGenericArguments()[0] };
      }
    }
    base.ResolveValueBinding(propertyItem);
  }
}