C# 如何在绑定到字典的Propertygrid中显示下拉列表

C# 如何在绑定到字典的Propertygrid中显示下拉列表,c#,propertygrid,C#,Propertygrid,我想在propertygrid中显示选定值的字符串下拉列表。 在我当前的例子中,我必须将dictionary对象绑定到属性网格 如果我正在绑定一个类,那么使用TypeConverter可以很容易地执行以下操作 public class Employee { public string Name { get; set; } [TypeConverter(typeof(JobCategoryConverter))] public int? Category { get; se

我想在propertygrid中显示选定值的字符串下拉列表。 在我当前的例子中,我必须将dictionary对象绑定到属性网格

如果我正在绑定一个类,那么使用TypeConverter可以很容易地执行以下操作

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;
}
结果是这样的

如果我绑定到字典,有什么建议可以显示这个下拉列表吗?我过去常常把字典和propertygrid绑定在一起

所以代码看起来像

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

这是相对容易的

其思想是允许为自定义
字典PropertyDescriptor
指定属性

首先,将
字典PropertyDescriptor
类构造函数更改为:

internal DictionaryPropertyDescriptor(IDictionary d, object key, Attribute[] attributes)
    : base(key.ToString(), attributes)
{
    _dictionary = d;
    _key = key;
}
然后将以下内容添加到
字典PropertyGridAdapter
类:

public Dictionary<string, Attribute[]> PropertyAttributes;
您已经完成了所需的更改

现在,您可以将
TypeConverter
与您的“属性”相关联,类似于您对此类所做的操作:

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;
}
enum JobCategory{accounter=1,工程师,经理}
类JobCategoryConverter:EnumConverter
{
public JobCategoryConverter():基(typeof(JobCategory)){}
}
私有void Form1\u加载(对象发送方、事件参数e)
{
IDictionary dict=新哈希表();
dict[“Name”]=“Ray”;
dict[“类别”]=1;
DictionaryPropertyGridAdapter dpg=新DictionaryPropertyGridAdapter(dict);
dpg.PropertyAttributes=新字典
{
{“类别”,新属性[]{new TypeConverterAttribute(typeof(JobCategoryConverter))}
};
propertyGrid1.SelectedObject=dpg;
}
结果将是:


您还可以关联其他属性,如
DisplayName
Category
等。

nice!!正是我想要的..让我对TypeConverter有了更多的了解你有没有机会分享你的
JobCatagoryConverter
课程?我正试图实现您已经能够实现的目标(即,将ID int映射到相关文本)。遗憾的是,您没有包含此代码。
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;
}