/**Dictionary looks like this
*[PERCENTAGELOSS, 0]
*[THRESHOLD, 1]
*etc.
*/
Dictionary<string, string> allEnums = XMLHandling.getEnums(enumCognityName);
foreach (KeyValuePair<string,string> item in allEnums)
{
enumCmb.Items.Add(item);
}
enumCmb.DisplayMember = "Key";
enumCmb.ValueMember = "Value";
enumCmb.SelectedIndex = 0;
}
将KeyValuePairs
添加到ComboBox
后,它会正确显示项目的名称,但我无法正确获取其值。这是尝试获取值的代码:
case "enum":
ComboBox enumBox = c as ComboBox;
var test0 = enumBox.SelectedItem; // [PERCENTAGELOSS, 0]
var test1 = enumBox.SelectedText; // ""
var test2 = enumBox.SelectedValue; // null
break;
我以为我正确地将ValueMember
添加为&#34;值&#34;来自KeyValuePair
。我做错了什么?
答案 0 :(得分:2)
您可以像这样绑定
,而不是在enumCmb中添加项目enumCmb.DisplayMember = "Key";
enumCmb.ValueMember = "Value";
enumCmb.DataSource = new BindingSource { DataSource = allEnums };
我不确定,但您可能需要将数据投放到列表中。