2009-12-18 39 views
11

我想使用propertygrid編輯鍵值(字符串,字符串)項目的列表。當我使用Dictionary<string,string>作爲類型時,propertygrid將顯示一個GUI,但它似乎不「啓用」,即。我無法添加任何項目。在propertygrid中使用字典

是否支持Dictionary對象,或者是否有任何其他對象可以解決此問題?

回答

18

我已經做了以下在過去this code

class DictionaryPropertyGridAdapter : ICustomTypeDescriptor 
{ 
    IDictionary _dictionary; 

    public DictionaryPropertyGridAdapter(IDictionary d) 
    { 
     _dictionary = d; 
    } 

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

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

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

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

    EventDescriptorCollection System.ComponentModel.ICustomTypeDescriptor.GetEvents() 
    { 
     return TypeDescriptor.GetEvents(this, true); 
    } 

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

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

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

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

    public PropertyDescriptor GetDefaultProperty() 
    { 
     return null; 
    } 

    PropertyDescriptorCollection 
     System.ComponentModel.ICustomTypeDescriptor.GetProperties() 
    { 
     return ((ICustomTypeDescriptor)this).GetProperties(new Attribute[0]); 
    } 

    public PropertyDescriptorCollection GetProperties(Attribute[] attributes) 
    { 
     ArrayList properties = new ArrayList(); 
     foreach (DictionaryEntry e in _dictionary) 
     { 
      properties.Add(new DictionaryPropertyDescriptor(_dictionary, e.Key)); 
     } 

     PropertyDescriptor[] props = 
      (PropertyDescriptor[])properties.ToArray(typeof(PropertyDescriptor)); 

     return new PropertyDescriptorCollection(props); 
    } 
} 

class DictionaryPropertyDescriptor : PropertyDescriptor 
{ 
    IDictionary _dictionary; 
    object _key; 

    internal DictionaryPropertyDescriptor(IDictionary d, object key) 
     : base(key.ToString(), null) 
    { 
     _dictionary = d; 
     _key = key; 
    } 

    public override Type PropertyType 
    { 
     get { return _dictionary[_key].GetType(); } 
    } 

    public override void SetValue(object component, object value) 
    { 
     _dictionary[_key] = value; 
    } 

    public override object GetValue(object component) 
    { 
     return _dictionary[_key]; 
    } 

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

    public override Type ComponentType 
    { 
     get { return null; } 
    } 

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

    public override void ResetValue(object component) 
    { 
    } 

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

private void Form1_Load(object sender, System.EventArgs e) 
{ 
    IDictionary d = new Hashtable(); 
    d["Hello"] = "World"; 
    d["Meaning"] = 42; 
    d["Shade"] = Color.ForestGreen; 

    propertyGrid1.SelectedObject = new DictionaryPropertyGridAdapter(d); 
} 

爲我們運作良好。

+0

感謝好友!你的代碼有很多幫助。 – 2017-06-29 11:02:27

2

如果你想綁定一個包含字典屬性設置爲PropertyGrid的,而不是字典本身直接結合的一類,然後在這裏是一個免費的(LGPL許可)組件正是這麼做的:http://gendictedit.codeplex.com/