0

Update: Just need to return a key from a dictionary, the dictionary is created by a web service, and passed back to my program which is a WPF, the dictionary is then populated into a dropdown that contains the following values

IT_ED
IT_FS
IT_BUS

The keys are assigned are as follows

Key Value
[1] IT_ED
[2] IT_FS
[3] IT_BUS

I need the key from the value, based on which item is selected in the drop down menu

Dictionary i use to house the keys

private Dictionary<int, string> _DivisionDict = new Dictionary<int, string>();

private Dictionary<int, string> DivisionDict
{
    get { return _DivisionDict; }
    set { _DivisionDict = value; }
}

Heres how I populate my drop down menu and the dictionary with the keys

foreach (var D in Divisions)
{
   txtY.Items.Add(D.Value);
   DivisionDict.Add(D.Key, D.Value);
}
Joe Doyle
  • 6,363
  • 3
  • 42
  • 45
Brad Fox
  • 685
  • 6
  • 19

1 Answers1

1

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

Community
  • 1
  • 1
NSGaga-mostly-inactive
  • 14,052
  • 3
  • 41
  • 51
  • Thanks for the answer, and the recommendation, but that doesnt seem to work, i use the dictionary in that method because its handled in the web service – Brad Fox Apr 12 '12 at 14:02
  • it really makes no sense what you're saying :) you need to scale things down and give us a specific example, small as possible – NSGaga-mostly-inactive Apr 12 '12 at 14:06
  • I updated my question, hopefully it makes more sense now, thanks – Brad Fox Apr 12 '12 at 14:24
  • return "a key" ? Do you need Divisions.Keys - or to query - or you don't know how to wrap a web service? – NSGaga-mostly-inactive Apr 12 '12 at 17:16
  • I need the key out of the division dictionary that matches the selected item – Brad Fox Apr 12 '12 at 17:49
  • ok getting closer - what's the "selected item" - you're getting it from GUI, else? – NSGaga-mostly-inactive Apr 12 '12 at 17:58
  • My list displays the values in a drop down thats on the front end – Brad Fox Apr 12 '12 at 19:18
  • 'Divisions[list.selected]' ?? - pseudo code - is it WPF? you aren't much of a talker aren't you? :). I still don't get it what you really want - and apparently nobody else does as there is none else responding to this. SO is warning us we should move to chat, so we should wrap up, try putting more data in - or you could make a small console test of what you want? Do you want to 'bind', with WPF data binding, is that it? Add comments up there in the question. – NSGaga-mostly-inactive Apr 12 '12 at 19:24
  • Alright made some final changes to the question... hopefully I dont look crazy now, I have issues explaining stuff well, i also changed my dictionary as ive been working on this all day, sorry and thanks – Brad Fox Apr 12 '12 at 20:32