Ok, that's better now (the question),
So, if I get it right you'd like to get back an 'index' from the selected item in the WPF dropdown (i.e. ComboBox).
First, easiest is to do a proper data 'binding' through your view-model,
ItemsSource="{Binding YourDictionaryProperty}"
SelectedItem="{Binding Path=YourDictionarySelectedItem, Mode=TwoWay, NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged}"
DisplayMemberPath="Value"
- Expose your dictionary through the View Model class. Like a property
you did create,
- put view model as your data context for the combobox (or parent etc.),
- both properties used for binding (
YourDictionarySelectedItem
and YourDictionaryProperty
) are properties in your view model. One exposes Dictionary 'as is', and another is KeyValuePair ...SelectedItem. Just FYI,
- do something like the above...
...then in your YourDicitonarySelecteItem
in your view model - you'll have the KeyValuePair from the dictionary. And from that you know both index and the value.
Or a brute force
would be to find the selected value (if you have that as 'output' based on how you currently set things up) in the dictionary via e.g. LINQ like this...
Divisions.Where(d=>d.Value == "selected value").FirstOrDefault();
...that'd give you the 'pair' and from that take the Key
.
hope we nailed it this time:), cheers
EDIT: proper dictionary binding was not in the original scope so it's a fast fix solution. If you need to update the dictionary 'from behind' you'd need to use some observable collection - or force OnPropertyChanged on the full dictionary whenever its items change. See this link for more specific info on that,
Two Way Data Binding With a Dictionary in WPF