2009-12-18 10 views
11

Vorrei modificare un elenco di elementi chiave (stringa, stringa) utilizzando un propertygrid. Quando uso un Dictionary<string,string> come tipo il propertygrid mostrerà una GUI, ma non sembra "abilitato", cioè. Non riesco ad aggiungere nessun articoloUtilizzo di un dizionario in un Propertygrid

L'oggetto Dizionario è supportato oppure esistono altri oggetti con cui è possibile risolvere questo problema?

risposta

18

l'ho fatto seguendo this code in passato:

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); 
} 

funzionato bene per noi.

+0

grazie amico ! il tuo codice è molto utile. –

2

Se si sta tentando di legare una classe che contiene una proprietà dizionario ad un PropertyGrid, piuttosto che legare il Dizionario per sé direttamente, quindi ecco un componente gratuito (LGPL licenza), che fa esattamente questo: http://gendictedit.codeplex.com/

Problemi correlati