简体   繁体   English

PropertyGrid-从IList派生的属性 <T> ,如何添加到PropertyGrid,以便用户可以添加/编辑/删除项目

[英]PropertyGrid - Property derived from IList<T>, How do I add to the PropertyGrid so user can Add/Edit/Remove items

Let me provide a bit of a history as to how I've reached this point. 让我提供一些有关如何达到这一点的历史。

I originally had a Property within my class that derived from CollectionsBase and had this collection mapped to the PropertyGrid and the user could Add/Edit/Remove items from the list at will. 我最初在类中有一个属性,该属性派生自CollectionsBase,并将此集合映射到PropertyGrid,并且用户可以随意从列表中添加/编辑/删除项目。

However, I couldn't map the CollectionsBase with NHibernate, thus I had to scrap my initial implementation and instead of deriving from CollectionsBase, I had the class derive from IList. 但是,我无法将NHibernate映射到CollectionsBase,因此我不得不取消最初的实现,而不是从CollectionsBase派生而是让类从IList派生。

Now I can map to NHibernate, but I am unable to edit the collection via the PropertyGrid. 现在,我可以映射到NHibernate,但是无法通过PropertyGrid编辑集合。

I need some help getting the 2 to play nice with each other. 我需要一些帮助,让2相互配合。

In my main class I have a property defined as: 在主类中,我有一个属性定义为:

    public virtual ZoneCollection Zones
    {
        get { return zones; }
        set { zones = value; }
    }

My Zone Collection that inherits IList is defined as follows: 继承IList的“我的区域集合”的定义如下:

public class ZoneCollection : IList<Zone>, ICustomTypeDescriptor
{
    private IList<Zone> _list;

    public IList<Zone> _List
    {
        get { return _list; }
    }

    public ZoneCollection()
    {
        _list = new List<Zone>();
    }

    #region Implementation of IEnumerable

    public IEnumerator<Zone> GetEnumerator()
    {
        return _list.GetEnumerator();
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }

    #endregion

    #region Implementation of ICollection<Zone>

    public void Add(Zone item)
    {
        _list.Add(item);
    }

    public void Clear()
    {
        _list.Clear();
    }

    public bool Contains(Zone item)
    {
        return _list.Contains(item);
    }

    public void CopyTo(Zone[] array, int arrayIndex)
    {
        _list.CopyTo(array, arrayIndex);
    }

    public bool Remove(Zone item)
    {
        return _list.Remove(item);
    }

    public int Count
    {
        get { return _list.Count; }
    }

    public bool IsReadOnly
    {
        get { return false; }
    }

    #endregion

    #region Implementation of IList<Zone>

    public int IndexOf(Zone item)
    {
        return _list.IndexOf(item);
    }

    public void Insert(int index, Zone item)
    {
        _list.Insert(index, item);
    }

    public void RemoveAt(int index)
    {
        _list.RemoveAt(index);
    }

    public Zone this[int index]
    {
        get { return (Zone)_list[index]; }
        set { _list[index] = value; }
    }

    #endregion

    // Implementation of interface ICustomTypeDescriptor 
    #region ICustomTypeDescriptor impl

    public String GetClassName()
    {
        return TypeDescriptor.GetClassName(this, true);
    }

    public AttributeCollection GetAttributes()
    {
        return TypeDescriptor.GetAttributes(this, true);
    }

    public String GetComponentName()
    {
        return TypeDescriptor.GetComponentName(this, true);
    }

    public TypeConverter GetConverter()
    {
        return TypeDescriptor.GetConverter(this, true);
    }

    public EventDescriptor GetDefaultEvent()
    {
        return TypeDescriptor.GetDefaultEvent(this, true);
    }

    public PropertyDescriptor GetDefaultProperty()
    {
        return TypeDescriptor.GetDefaultProperty(this, true);
    }

    public object GetEditor(Type editorBaseType)
    {
        return TypeDescriptor.GetEditor(this, editorBaseType, true);
    }

    public EventDescriptorCollection GetEvents(Attribute[] attributes)
    {
        return TypeDescriptor.GetEvents(this, attributes, true);
    }

    public EventDescriptorCollection GetEvents()
    {
        return TypeDescriptor.GetEvents(this, true);
    }

    public object GetPropertyOwner(PropertyDescriptor pd)
    {
        return this;
    }


    /// <summary>
    /// Called to get the properties of this type. Returns properties with certain
    /// attributes. this restriction is not implemented here.
    /// </summary>
    /// <param name="attributes"></param>
    /// <returns></returns>
    public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
    {
        return GetProperties();
    }

    /// <summary>
    /// Called to get the properties of this type.
    /// </summary>
    /// <returns></returns>
    public PropertyDescriptorCollection GetProperties()
    {
        // Create a collection object to hold property descriptors
        PropertyDescriptorCollection pds = new PropertyDescriptorCollection(null);

        // Iterate the list of zones
        for (int i = 0; i < this._list.Count; i++)
        {
            // Create a property descriptor for the zone item and add to the property descriptor collection
            ZoneCollectionPropertyDescriptor pd = new ZoneCollectionPropertyDescriptor(this, i);
            pds.Add(pd);
        }
        // return the property descriptor collection
        return pds;
    }

    #endregion
}

/// <summary>
/// Summary description for CollectionPropertyDescriptor.
/// </summary>
public class ZoneCollectionPropertyDescriptor : PropertyDescriptor
{
    private ZoneCollection collection = null;
    private int index = -1;

    public ZoneCollectionPropertyDescriptor(ZoneCollection coll, int idx) :
        base("#" + idx.ToString(), null)
    {
        this.collection = coll;
        this.index = idx;
    }

    public override AttributeCollection Attributes
    {
        get
        {
            return new AttributeCollection(null);
        }
    }

    public override bool CanResetValue(object component)
    {
        return true;
    }

    public override Type ComponentType
    {
        get
        {
            return this.collection.GetType();
        }
    }

    public override string DisplayName
    {
        get
        {
            Zone zone = this.collection[index];
            return zone.ID.ToString();
        }
    }

    public override string Description
    {
        get
        {
            Zone zone = this.collection[index];
            StringBuilder sb = new StringBuilder();
            sb.Append(zone.ID.ToString());

            if (zone.Streets.Route != String.Empty || zone.Streets.Crossing != String.Empty)
                sb.Append("::");
            if (zone.Streets.Route != String.Empty)
                sb.Append(zone.Streets.Route);
            if (zone.Streets.Crossing != String.Empty)
            {
                sb.Append(" and ");
                sb.Append(zone.Streets.Crossing);
            }

            return sb.ToString();
        }
    }

    public override object GetValue(object component)
    {
        return this.collection[index];
    }

    public override bool IsReadOnly
    {
        get { return false; }
    }

    public override string Name
    {
        get { return "#" + index.ToString(); }
    }

    public override Type PropertyType
    {
        get { return this.collection[index].GetType(); }
    }

    public override void ResetValue(object component)
    {
    }

    public override bool ShouldSerializeValue(object component)
    {
        return true;
    }

    public override void SetValue(object component, object value)
    {
        // this.collection[index] = value;
    }
}

} }

Now, my ICustomTypeDescriptor and PropertyDescriptor worked fine when this class derived from CollectionsBase, but now it just shows the class name ZoneCollection in the property name without the "..." button to add/edit/remove the items from the list. 现在,当从CollectionsBase派生的此类时,我的ICustomTypeDescriptor和PropertyDescriptor可以正常工作,但现在它仅在属性名称中显示类名称ZoneCollection,而无需使用“ ...”按钮来添加/编辑/从列表中删除项目。

What am I doing wrong now that it is inherited from IList that this isn't working? 从IList继承而来的这不起作用,现在我在做什么错呢?

If I add: 如果我添加:

[TypeConverter(typeof(ExpandableObjectConverter))]

To the beginning of the ZoneCollection, I get the items in the list listed in an expandable tree, but that's not what I am looking for. 在ZoneCollection的开头,我得到了可扩展树中列出的列表中的项目,但这不是我想要的。 Where did the "..." button go that opened up a popup window that enabled me to add/edit/remove the items in the collection when I inherited from IList instead of CollectionBase? “ ...”按钮在哪里打开了一个弹出窗口,当我从IList而不是CollectionBase继承时,该窗口使我能够添加/编辑/删除集合中的项目?

The PropertyGrid is an old grumpy beast. PropertyGrid是一个古老的脾气暴躁的野兽。 It needs the non-generic IList explicit implementation, not the generic one. 它需要非通用的IList显式实现,而不是通用的。

As a site note, you could derive ZoneCollection directly from List<Zone> , and you don't need any ICustomTypeDescriptor / PropertyDescriptor, with regards to this PropertyGrid issue. 作为站点说明,您可以直接从List<Zone>派生ZoneCollection,并且就此PropertyGrid问题而言,不需要任何ICustomTypeDescriptor / PropertyDescriptor。

Here is an implementation that seems to work: 这是一个似乎可行的实现:

public class ZoneCollection : IList<Zone>, IList
{
    private List<Zone> _list = new List<Zone>();

    public ZoneCollection()
    {
    }

    public int IndexOf(Zone item)
    {
        return _list.IndexOf(item);
    }

    public void Insert(int index, Zone item)
    {
        _list.Insert(index, item);
    }

    public void RemoveAt(int index)
    {
        _list.RemoveAt(index);
    }

    public Zone this[int index]
    {
        get
        {
            return _list[index];
        }
        set
        {
            _list[index] = value;
        }
    }

    public void Add(Zone item)
    {
        _list.Add(item);
    }

    public void Clear()
    {
        _list.Clear();
    }

    public bool Contains(Zone item)
    {
        return _list.Contains(item);
    }

    public void CopyTo(Zone[] array, int arrayIndex)
    {
        _list.CopyTo(array, arrayIndex);
    }

    public int Count
    {
        get { return _list.Count; }
    }

    public bool IsReadOnly
    {
        get { return ((IList)_list).IsReadOnly; }
    }

    public bool Remove(Zone item)
    {
        return _list.Remove(item);
    }

    public IEnumerator<Zone> GetEnumerator()
    {
        return _list.GetEnumerator();
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }

    int IList.Add(object value)
    {
        int index = Count;
        Add((Zone)value);
        return index;
    }

    bool IList.Contains(object value)
    {
        return Contains((Zone)value);
    }

    int IList.IndexOf(object value)
    {
        return IndexOf((Zone)value);
    }

    void IList.Insert(int index, object value)
    {
        Insert(index, (Zone)value);
    }

    bool IList.IsFixedSize
    {
        get { return ((IList)_list).IsFixedSize; }
    }

    bool IList.IsReadOnly
    {
        get { return ((IList)_list).IsReadOnly; }
    }

    void IList.Remove(object value)
    {
        Remove((Zone)value);
    }

    object IList.this[int index]
    {
        get
        {
            return this[index];
        }
        set
        {
            this[index] = (Zone)value;
        }
    }

    void ICollection.CopyTo(Array array, int index)
    {
        CopyTo((Zone[])array, index);
    }

    bool ICollection.IsSynchronized
    {
        get { return ((ICollection)_list).IsSynchronized; }
    }

    object ICollection.SyncRoot
    {
        get { return ((ICollection)_list).SyncRoot; }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM