2016-03-24 4 views
2

Ich möchte ein Dropdown-Feld mit Strings in einem PropertyGrid für einen ausgewählten Wert anzeigen. In meinem aktuellen Fall muss ich ein Dictionary-Objekt an ein Eigenschaftsraster binden.So zeigen Sie das Dropdown in einer Propertygrid-Bindung für das Dictionary an

Wenn ich eine Klasse binde, ist es einfach, wie folgt mit einem TypeConverter zu tun.

public class Employee 
{ 
    public string Name { get; set; } 
    [TypeConverter(typeof(JobCategoryConverter))] 
    public int? Category { get; set; }   
} 

private void Form1_Load(object sender, EventArgs e) 
{ 
    Employee emp = new Employee() {Name = "Ray" ,Category = 1 }; 
    propertyGrid1.SelectedObject = emp; 
} 

Ergebnis wie folgt aussehen.

enter image description here

Jeder Vorschlag, wie ich diesen Drop-Down zeigen kann, wenn ich in ein Wörterbuch zu binden? Ich benutzte this code, um Wörterbuch an propertygrid zu binden.

So sieht der Code wie,

private void Form1_Load(object sender, EventArgs e) 
{ 
    IDictionary dict = new Hashtable(); 
    dict["Name"] = "Ray"; 
    dict["Category"] = 1; 

    DictionaryPropertyGridAdapter dpg = new  DictionaryPropertyGridAdapter(dict); 
    propertyGrid1.SelectedObject = dpg; 
} 

Antwort

2

Diese relativ einfach ist.

Die Idee besteht darin, Attribute für die benutzerdefinierte DictionaryPropertyDescriptor zu ermöglichen.

zuerst die DictionaryPropertyDescriptor Klasse ändern Konstruktor:

internal DictionaryPropertyDescriptor(IDictionary d, object key, Attribute[] attributes) 
    : base(key.ToString(), attributes) 
{ 
    _dictionary = d; 
    _key = key; 
} 

zum DictionaryPropertyGridAdapter Klasse die folgende Dann fügen:

public Dictionary<string, Attribute[]> PropertyAttributes; 

und ändern Sie die GetProperties Methode:

public PropertyDescriptorCollection GetProperties(Attribute[] attributes) 
{ 
    ArrayList properties = new ArrayList(); 
    foreach (DictionaryEntry e in _dictionary) 
    { 
     Attribute[] attrs; 
     if (PropertyAttributes == null || !PropertyAttributes.TryGetValue(e.Key.ToString(), out attrs)) 
      attrs = null; 
     properties.Add(new DictionaryPropertyDescriptor(_dictionary, e.Key, attrs)); 
    } 

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

    return new PropertyDescriptorCollection(props); 
} 

und Sie sind mit den erforderlichen Änderungen fertig.

Jetzt können Sie TypeConverter mit Ihrer „Eigenschaft“ ähnlich assoziieren, was du so mit einer Klasse zu tun:

enum JobCategory { Accountant = 1, Engineer, Manager } 

class JobCategoryConverter : EnumConverter 
{ 
    public JobCategoryConverter() : base(typeof(JobCategory)) { } 
} 

private void Form1_Load(object sender, EventArgs e) 
{ 
    IDictionary dict = new Hashtable(); 
    dict["Name"] = "Ray"; 
    dict["Category"] = 1; 

    DictionaryPropertyGridAdapter dpg = new DictionaryPropertyGridAdapter(dict); 
    dpg.PropertyAttributes = new Dictionary<string, Attribute[]> 
    { 
     { "Category", new Attribute[] { new TypeConverterAttribute(typeof(JobCategoryConverter)) } } 
    }; 
    propertyGrid1.SelectedObject = dpg; 
} 

und das Ergebnis wird sein:

enter image description here

Sie können auch assoziiere andere Attribute wie DisplayName, Category usw.

+0

nett !! genau das, was ich wollte, gibt mir mehr über die TypeConverter-Sache – vinayan

Verwandte Themen