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

2
  • Are you using Bindings or did you add a couple of ComboBoxItems manually? Commented Feb 1, 2017 at 7:42
  • 1
    @ManfredRadlwimmer Manually Commented Feb 1, 2017 at 7:43

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

3 Comments

@Tushar: Could you please take a look into the updates
As i already said brother, same above exception is occurring and the code that I had writtern in problem and the code that you has answered both are same
try this string item = cmbOptions.SelectedItem.ToString() and also take a look into the additional notes in my post
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.

2 Comments

You should remove the not working (commented) block. Maybe you could explain the difference between 'SelectedValue' and 'SelectedItem'?
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...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.