Instead of adding items in enumCmb, you can bind like this
enumCmb.DisplayMember = "Key";
enumCmb.ValueMember = "Value";
enumCmb.DataSource = new BindingSource { DataSource = allEnums };
I'm not sure but you may need to cast your data to list.
Question
/**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;
}
After I add the KeyValuePairs
to the ComboBox
, it correctly displays the names of the items, but I can't get their values properly. Here's the code that tries to get the values:
case "enum":
ComboBox enumBox = c as ComboBox;
var test0 = enumBox.SelectedItem; // [PERCENTAGELOSS, 0]
var test1 = enumBox.SelectedText; // ""
var test2 = enumBox.SelectedValue; // null
break;
I thought I correctly added the ValueMember
as the "Value" from the KeyValuePair
. What am I doing wrong?
Solution
Instead of adding items in enumCmb, you can bind like this
enumCmb.DisplayMember = "Key";
enumCmb.ValueMember = "Value";
enumCmb.DataSource = new BindingSource { DataSource = allEnums };
I'm not sure but you may need to cast your data to list.