2012-07-17 85 views
0

我開始了一個新的項目,我碰到這個線程: Is the CollectionBase class still supported?PropertyGrid中和ICollection的

所以我我collectionbase類轉換成通用的ICollection類。 我的問題是,我正在使用propertygrid視圖與collectionbase 一起工作,並沒有與我的新班級工作!

Add按鈕被禁用:(

[Editor(typeof(System.ComponentModel.Design.CollectionEditor), typeof(System.Drawing.Design.UITypeEditor))] 
public class SomeCollection<T> : ICollection<T> where T : SomeClass 
{ 
    List<T> SomeList; 

    private bool _IsReadOnly = false; 
    public bool IsReadOnly 
    { 
     get 
     { 
      return _IsReadOnly; 
     } 
    } 

    public SomeCollection() { SomeList = new List<T>(); } 

    public T this[int index] 
    { 
     get 
     { 
      return (T)SomeList[index]; 

     } 
     set 
     { 
      SomeList[index] = value; 
     } 
    } 

    public void Add(T b) 
    { 
     if (!exists(b.Name)) 
     { 
      SomeList.Add(b); 
     } 
     else 
     { 
      throw new ArgumentException("bla"); 
     } 
    } 

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

    public bool Remove(T b) 
    { 
     bool Removed = false; 
     if (SomeList.Contains(b)) 
     { 
      SomeList.Remove(b); 
      Removed = true; 
     } 
     return Removed; 
    } 

    public int IndexOf(T b) 
    { 
     return SomeList.IndexOf(b); 
    } 

    public void CopyTo(T[] array, int arrayIndex) 
    { 
     for (int i = 0; i < SomeList.Count; i++) 
     { 

      array[i] = (T)SomeList[i]; 
     } 
    } 

    public void AddRange(SomeCollection<T> Collection) 
    { 
     for (int i = 0; i < Collection.Count(); i++) 
     { 
      SomeList.Add(Collection[i]); 
     } 
    } 

    public void AddRange(T[] Collection) 
    { 
     SomeList.AddRange(Collection); 
    } 

    public bool Contains(T b) 
    { 
     return SomeList.Contains(b); 
    } 

    public void Insert(int index, T b) 
    { 
     SomeList.Insert(index, b); 
    } 

    public override string ToString() 
    { 
     if (SomeList.Count() > 0) 
     { 
      return string.Format("SomeClass ({0})", new object[] { SomeList.Count() }); 
     } 
     else 
     { 
      return string.Empty; 
     } 
    } 

    public bool exists(string name) 
    { 
     bool _found = false; 
     if (name != null) 
     { 
      for (int i = 0; i < SomeList.Count; i++) 
      { 
       if (((SomeClass)SomeList[i]).Address.Equals(name, StringComparison.OrdinalIgnoreCase)) 
       { 
        _found = true; 
        break; 
       } 
      } 
     } 
     return _found; 

    } 

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

    public IEnumerator<T> GetEnumerator() 
    { 
     return new SomeClassEnumerator<T>(this); 
    } 

    IEnumerator IEnumerable.GetEnumerator() 
    { 
     return new SomeClassEnumerator<T>(this); 
    } 
} 

回答

0

IIRC,你需要實現IList(不是仿製藥)。