2011-01-04 13 views
0

私はPropertyGridの中でカスタムコレクションを使用するための参照としてこの記事を使用しています: LINKカスタムコレクション

私はcollectioneditorを開いて、すべての項目を削除すると、私はOKを押し、私がnullの場合は例外を取得。

どうすれば解決できますか?
私が使用しています:項目のためのゲッターとして

public T this[int index] 
{ 
    get 
    { 
     if (List.Count == 0) 
     { 
     return default(T); 
     } 
     else 
     { 
     return (T)this.List[index]; 
     } 
    } 
} 

を、もちろん私はコレクション全体を再起動することができますどのように何のオブジェクトを持っていない場合は?

、これは全体のコードです

/// <summary> 
/// A generic folder settings collection to use in a property grid. 
/// </summary> 
/// <typeparam name="T">can be import or export folder settings.</typeparam> 
[Serializable] 
[TypeConverter(typeof(FolderSettingsCollectionConverter)), Editor(typeof(FolderSettingsCollectionEditor), typeof(UITypeEditor))] 
public class FolderSettingsCollection_New<T> : CollectionBase, ICustomTypeDescriptor 
{ 
    private bool m_bRestrictNumberOfItems; 
    private int m_bNumberOfItems; 
    private Dictionary<string, int> m_UID2Idx = new Dictionary<string, int>(); 
    private T[] arrTmp; 

    /// <summary> 
    /// C'tor, can determine the number of objects to hold. 
    /// </summary> 
    /// <param name="bRestrictNumberOfItems">restrict the number of folders to hold.</param> 
    /// <param name="iNumberOfItems">The number of folders to hold.</param> 
    public FolderSettingsCollection_New(bool bRestrictNumberOfItems = false , int iNumberOfItems = 1) 
    { 
     m_bRestrictNumberOfItems = bRestrictNumberOfItems; 
     m_bNumberOfItems = iNumberOfItems; 
    } 

    /// <summary> 
    /// Add folder to collection. 
    /// </summary> 
    /// <param name="t">Folder to add.</param> 
    public void Add(T t) 
    { 
     if (m_bRestrictNumberOfItems) 
     { 
      if (this.List.Count >= m_bNumberOfItems) 
      { 
       return; 
      } 
     } 

     int index = this.List.Add(t); 

     if (t is WriteDataFolderSettings || t is ReadDataFolderSettings) 
     { 
      FolderSettingsBase tmp = t as FolderSettingsBase; 
      m_UID2Idx.Add(tmp.UID, index); 
     } 
    } 

    /// <summary> 
    /// Remove folder to collection. 
    /// </summary> 
    /// <param name="t">Folder to remove.</param> 
    public void Remove(T t) 
    { 
     this.List.Remove(t); 

     if (t is WriteDataFolderSettings || t is ReadDataFolderSettings) 
     { 
      FolderSettingsBase tmp = t as FolderSettingsBase; 
      m_UID2Idx.Remove(tmp.UID); 
     } 
    } 

    /// <summary> 
    /// Gets ot sets a folder. 
    /// </summary> 
    /// <param name="index">The index of the folder in the collection.</param> 
    /// <returns>A folder object.</returns> 
    public T this[int index] 
    { 
     get 
     { 
      //if (List.Count == 0) 
      //{ 
      // return default(T); 
      //} 
      //else 
      //{ 
       return (T)this.List[index]; 
      //} 
     } 
    } 

    /// <summary> 
    /// Gets or sets a folder. 
    /// </summary> 
    /// <param name="sUID">The UID of the folder.</param> 
    /// <returns>A folder object.</returns> 
    public T this[string sUID] 
    { 
     get 
     { 
      if (this.Count == 0 || !m_UID2Idx.ContainsKey(sUID)) 
      { 
       return default(T); 
      } 
      else 
      { 
       return (T)this.List[m_UID2Idx[sUID]]; 
      } 
     } 
    } 

    /// <summary> 
    /// 
    /// </summary> 
    /// <param name="sUID"></param> 
    /// <returns></returns> 
    public bool ContainsItemByUID(string sUID) 
    { 
     return m_UID2Idx.ContainsKey(sUID); 
    } 

    /// <summary> 
    /// 
    /// </summary> 
    /// <returns></returns> 
    public String GetClassName() 
    { 
     return TypeDescriptor.GetClassName(this, true); 
    } 

    /// <summary> 
    /// 
    /// </summary> 
    /// <returns></returns> 
    public AttributeCollection GetAttributes() 
    { 
     return TypeDescriptor.GetAttributes(this, true); 
    } 

    /// <summary> 
    /// 
    /// </summary> 
    /// <returns></returns> 
    public String GetComponentName() 
    { 
     return TypeDescriptor.GetComponentName(this, true); 
    } 

    /// <summary> 
    /// 
    /// </summary> 
    /// <returns></returns> 
    public TypeConverter GetConverter() 
    { 
     return TypeDescriptor.GetConverter(this, true); 
    } 

    /// <summary> 
    /// 
    /// </summary> 
    /// <returns></returns> 
    public EventDescriptor GetDefaultEvent() 
    { 
     return TypeDescriptor.GetDefaultEvent(this, true); 
    } 

    /// <summary> 
    /// 
    /// </summary> 
    /// <returns></returns> 
    public PropertyDescriptor GetDefaultProperty() 
    { 
     return TypeDescriptor.GetDefaultProperty(this, true); 
    } 

    /// <summary> 
    /// 
    /// </summary> 
    /// <param name="editorBaseType"></param> 
    /// <returns></returns> 
    public object GetEditor(Type editorBaseType) 
    { 
     return TypeDescriptor.GetEditor(this, editorBaseType, true); 
    } 

    /// <summary> 
    /// 
    /// </summary> 
    /// <param name="attributes"></param> 
    /// <returns></returns> 
    public EventDescriptorCollection GetEvents(Attribute[] attributes) 
    { 
     return TypeDescriptor.GetEvents(this, attributes, true); 
    } 

    /// <summary> 
    /// 
    /// </summary> 
    /// <returns></returns> 
    public EventDescriptorCollection GetEvents() 
    { 
     return TypeDescriptor.GetEvents(this, true); 
    } 


    /// <summary> 
    /// 
    /// </summary> 
    /// <param name="pd"></param> 
    /// <returns></returns> 
    public object GetPropertyOwner(PropertyDescriptor pd) 
    { 
     return this; 
    } 

    /// <summary> 
    /// 
    /// </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 employees 
     for (int i = 0; i < this.List.Count; i++) 
     { 
      // Create a property descriptor for the employee item and add to the property descriptor collection 
      CollectionPropertyDescriptor_New<T> pd = new CollectionPropertyDescriptor_New<T>(this, i); 
      pds.Add(pd); 
     } 
     // return the property descriptor collection 
     return pds; 
    } 

    public T[] ToArray() 
    { 
     if (arrTmp == null) 
     { 
      arrTmp = new T[List.Count]; 
      for (int i = 0; i < List.Count; i++) 
      { 
       arrTmp[i] = (T)List[i]; 
      } 
     } 

     return arrTmp; 
    } 

} 

/// <summary> 
/// Enable to display data about a collection in a property grid. 
/// </summary> 
/// <typeparam name="T">Folder object.</typeparam> 
public class CollectionPropertyDescriptor_New<T> : PropertyDescriptor 
{ 
    private FolderSettingsCollection_New<T> collection = null; 
    private int index = -1; 
    /// <summary> 
    /// 
    /// </summary> 
    /// <param name="coll"></param> 
    /// <param name="idx"></param> 
    public CollectionPropertyDescriptor_New(FolderSettingsCollection_New<T> coll, int idx) : base("#" + idx.ToString(), null) 
    { 
     this.collection = coll; 
     this.index = idx; 
    } 

    /// <summary> 
    /// 
    /// </summary> 
    public override AttributeCollection Attributes 
    { 
     get 
     { 
      return new AttributeCollection(null); 
     } 
    } 
    /// <summary> 
    /// 
    /// </summary> 
    /// <param name="component"></param> 
    /// <returns></returns> 
    public override bool CanResetValue(object component) 
    { 
     return true; 
    } 

    /// <summary> 
    /// 
    /// </summary> 
    public override Type ComponentType 
    { 
     get 
     { 
      return this.collection.GetType(); 
     } 
    } 

    /// <summary> 
    /// 
    /// </summary> 
    public override string DisplayName 
    { 
     get 
     { 
      if (this.collection[index] != null) 
      { 
       return this.collection[index].ToString(); 
      } 
      else 
      { 
       return null; 
      } 
     } 
    } 

    public override string Description 
    { 
     get 
     { 
      return ""; 
     } 
    } 

    /// <summary> 
    /// 
    /// </summary> 
    /// <param name="component"></param> 
    /// <returns></returns> 
    public override object GetValue(object component) 
    { 
     if (this.collection[index] != null) 
     { 
      return this.collection[index]; 
     } 
     else 
     { 
      return null; 
     } 
    } 

    /// <summary> 
    /// 
    /// </summary> 
    public override bool IsReadOnly 
    { 
     get { return false; } 
    } 

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

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

    public override void ResetValue(object component) 
    { 

    } 

    /// <summary> 
    /// 
    /// </summary> 
    /// <param name="component"></param> 
    /// <returns></returns> 
    public override bool ShouldSerializeValue(object component) 
    { 
     return true; 
    } 

    /// <summary> 
    /// 
    /// </summary> 
    /// <param name="component"></param> 
    /// <param name="value"></param> 
    public override void SetValue(object component, object value) 
    { 
     // this.collection[index] = value; 
    } 
} 

まあみんな助けありがとうございました。

私はついに戦いに勝った。

(index = 0)== nullの場合、表示名はnullを返しました。

の代わりに、String.Emptyを...これはあなたは、単に使用する場合は、あなたのいくつかの

+0

私は、デフォルト値を返しません空のListの場合はT(nullならTはnull)のほうが少し奇妙なようです。とにかく、あなたの問題はあなたのコードの別のポイントにあると思います。 – digEmAll

+0

私はあなたに感謝しました。 – guyl

+0

Mmh ...明らかに間違っているものは何も見つかりませんでした(しかし、それをデバッグするのは難しいです)ので、ステップバイステップでデバッグすることをお勧めします(設計時にも - http: //msdn.microsoft.com/en-us/library/5ytx0z24%28v=VS.90%29.aspx)。 – digEmAll

答えて

0

何を得るのですか助ける

希望:

public T this[int index] 
{ 
    get 
    { 
     return (T)this.List[index]; 
    } 
} 
+0

項目が定義されていないときにthis.List [index]がnullの場合、それでも例外が発生します。私はそれを元の状態に戻す必要があります。 – guyl

+0

コードの別の部分に問題があります。リストに値がない場合、コレクションエディタは 'this [index]'をコールしてはいけません。おそらくあなたは 'Count'プロパティと内部リストの間に同期問題があります... – digEmAll

+0

彼のコードにバグがあります。アイテムを削除する前にコレクションを展開していなければ動作します。 – lesscode

関連する問題