运行时的集合编辑器

时间:2020-03-06 15:03:36  来源:igfitidea点击:

我正在使用属性网格编辑名称/值对的应用程序。
我的类文件中的某些属性是ListDictionary集合。在那儿
我可以在属性声明中应用的Editor属性,该属性将使
集合编辑器在运行时可以工作吗?如果不是,是否有可能继承自
是否在运行时使用ComponentModel.Design.CollectionEditor?我需要能够添加
删除并编辑集合值。
非常感谢,
特里

解决方案

我认为这篇文章解释了需求:如何使用CollectionEditor编辑和保留集合

来自codeproject文章[http://www.codeproject.com/KB/cs/dzcollectioneditor.aspx][1]

There are three requirements that a
  collection should meet in order to be
  successfully persisted with the
  CollectionEditor:
  
  
  First, the collection must implement the IList interface
  (inheriting from
  System.Collections.CollectionBase is
  in most of the cases the best option).
  Second, it must have an Indexer (Item in VB.NET) property. The type of
  this property is used by the
  CollectionEditor to determine the
  default type of the instances that
  will add to the collection.
  
  To better understand how this works, take a look at GetItemType()
  function of the
  CustomCollectionEditorForm:
  
  protected virtual Type GetItemType(IList coll)
    {
        PropertyInfo pi= coll.GetType().GetProperty("Item",
                                               new Type[]{typeof(int)});
        return pi.PropertyType
    }
  Third, the collection class must implement one or both of the following
  methods: Add and AddRange. Although
  IList interface has an Add member and
  CollectionBase implements IList, you
  still have to implement an Add method
  for your collection, given that
  CollectionBase declares an explicit
  member implementation of the IList’s
  Add member. The designer serializes
  the collection according to what
  method you have implemented. If you
  have implemented both, the AddRange is
  preferred.

在本文中,我们将找到在属性网格上实现集合所需的一切