1

I have a combobox cmbOptions and a button btnShowItem

and here's the code:

private void btnShowItem_click(object sender, RoutedEventArgs e)
{
    string item = ((ComboBoxItem)cmbOptions.SelectedItem).Content.ToString(); //Exception is here
}

Below is the exception:

System.InvalidCastException: "Unable to cast object of type 'System.String' to type 'System.Windows.Controls.ComboBoxItem'."

I've already gone through a number of links like these:

Cannot get ComboBox selected item value

ComboBox- SelectionChanged event has old value, not new value

Get selected value from combo box in c# wpf

etc, etc..

But didn't get the solution.

Please note I need to get the value of comboboxItem on buttonclick only NOT on cmbSelectionChange event

Community
  • 1
  • 1
Tk1993
  • 501
  • 8
  • 21

2 Answers2

4

By using .Content.ToString() the entire thing is converted to string, and you are trying to cast this resultant string to ComboBoxItem such conversion is not permitted, But you can cast the SelectedItem to a ComboBoxItem and then take values from them. try something like this:

ComboBoxItem currentItem = (ComboBoxItem)cmbOptions.SelectedItem; // this will be the comboBoxItem
string item =currentItem.Content.ToString(); // gives you the required string

If you combine both the steps you can write like this:

string item =((ComboBoxItem)cmbOptions.SelectedItem).Content.ToString(); 

Additional note:

Still, you are getting the same exception means SelectedItem will be a string, try to get the value like this: string item = cmbOptions.SelectedItem.ToString(), This will happen because you may assign the DisplayMemberPath

sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
0
        for (int x = 0; x < cboType.Items.Count; x++)
        {
            cboType.SelectedIndex = x;
            var typeCombo = ((ComboBox)cboType);
            var valueType = ((ComboBoxItem)typeCombo.SelectedValue);

            if (thisProductInfo.Type == valueType.Content.ToString())
            {
                cboType.SelectedIndex = x;
                break;
            }
        }

        //for (int x = 0; x < cboColor.Items.Count; x++)
        //{
        //    cboColor.SelectedIndex = x;
        //    var colorCombo = ((ComboBox)cboColor);
        //    var valueColor = ((ComboBoxItem)colorCombo.SelectedValue);

        //    if (thisProductInfo.Type == valueColor.Content.ToString())
        //    {
        //        cboColor.SelectedIndex = x;
        //        break;
        //    }
        //}

how about this one? the former works but the commented loop gives me an error of casting, tried selectedindex but same results, only the former works.

user11401
  • 89
  • 9
  • You should remove the not working (commented) block. Maybe you could explain the difference between 'SelectedValue' and 'SelectedItem'? – Tobias Otto Mar 01 '18 at 17:36
  • nah, im here to find answers, I dont know why the codes below wont work while the codes above does but they're basically the same.. very weird... – user11401 Mar 02 '18 at 00:22