Search code examples
c#wpfvisual-studiocombobox

How to retrieve selected value of combobox (not text)


I've been making a little app to do some user account creations in AD, but I seem to be stuck with my ComboBox.

Currently in my project there's a method to add items to the specific ComboBox:

private void AddAccessBox(string name, string value)
    {
        ComboboxItem newitem = new ComboboxItem();
        newitem.Text = name;
        newitem.Value = value;
        accessLevelBox.Items.Add(newitem);
    }


To add an item to the ComboBox:

AddAccessBox("Standard User", "SSLVPN,anothergroup,andanother");


For some ungodly reason I can't work out how to get the Value of the selected item, SelectedValue and SelectedItem both are returning "Standard User".

enter image description here


I'm sure there's something tiny that i'm missing, any help would be greatly appreciated.


Solution

  • You have assigned a ComboboxItem instance to the Items collection of the ComboBox, so a ComboboxItem is returned by SelectedItem (or SelectedValue):

    ComboboxItem item = accessLevelCombobox.SelectedItem as ComboboxItem;
    
    if (item != null && item.Value == "SSLVPN,anothergroup,andanother")
    {
    }