In cases like this, dictionaries are simply collections of key-value pairs, so each item on the ComboBox
is a KeyValuePair<YourKeyType, YourValueType>
. Cast SelectedItem
to a KeyValuePair<YourKeyType, YourValueType>
and then you can read the key.
// get ComboBox from sender
ComboBox comboBox = (ComboBox) sender;
// get selected KVP
KeyValuePair<YourKeyType, YourValueType> selectedEntry
= (KeyValuePair<YourKeyType, YourValueType>) comboBox.SelectedItem;
// get selected Key
YourKeyType selectedKey = selectedEntry.Key;
Or, a simpler way is to use the SelectedValue
property.
// get ComboBox from sender
ComboBox comboBox = (ComboBox) sender;
// get selected Key
YourKeyType selectedKey = (YourKeyType) comboBox.SelectedValue;