将字典绑定到Combobox [英] Bind a Dictionary to Combobox
问题描述
嗨朋友们......
将字典绑定到COmbobox的最佳方法是什么?
我用过这个...
Hi friends...
Whats the best way to bind a Dictionary to COmbobox?
I have used this...
Dictionary<string,string> ldict = new Dictionary<string,string>();
ldict.Add("English", "En.dll");
ldict.Add("German", "Ge.dll");
ldict.Add("French", "Fr.dll");
comboBox2.DataSource = ldict.ToList();
comboBox2.ValueMember = "Value";
comboBox2.DisplayMember = "Key";
combobox2.SelectedValue给出了正确的选定值....但我无法获得选定的密钥...
如果我调用combobox2.SelectedItem它返回整个Dictionary对象....
请建议我该怎么做...
谢谢
combobox2.SelectedValue gives me the correct selected value.... But I cannot get the Selected Key...
If I call combobox2.SelectedItem It returns the whole Dictionary object....
Please suggest me what should I do...
Thanks
推荐答案
我会做以下事情:
I would do the following:
Dictionary<string,string> ldict = new Dictionary<string,string>();
ldict.Add("English", "En.dll");
ldict.Add("German", "Ge.dll");
ldict.Add("French", "Fr.dll");
comboBox2.DataSource = ldict.ToList();
comboBox2.ValueMember = "Key";
comboBox2.DisplayMember = "Key";
然后在选择更改中,您可以使用已选择的键从字典中获取值。可能有更好的方法,但我不确定它会是什么。
Then in the select change you can get the value from the dictionary using the key that has been selected. There is probably a better way but I'm not sure what it would be.
这篇关于将字典绑定到Combobox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!