How to select a item in a Dictionary, using a list box
I have a listBox visually representing the items in a Dictionary. The Dictionary contains a custom class called开发者_JAVA百科 Contact.
I want to use the strings in the listBox (which are the same as a Dicitonary reference) to refer to the Contact contained in the Dictionary. I am having trouble getting this working, if it is even possible.
The listbox can be databound to the keys property of the Dictionary. You can then get the selected value of the list box and use that as a key to the dictionary to get the associated Contact object.
as long as the text of the listBox is the key of the item in the dictionary, things should be fairly straightforward. (i may have mucked up the property names here)
var contact = dict[listBox.SelectedItem];
One of the possibilities is that you can bind your dictionary to DataSource
with proper DisplayMember
, then just simply get whole Contact
objects.
精彩评论