Alan Coromano Alan Coromano - 1 month ago 10
C# Question

Cannot get a selected text or value in ComboBox

I have a combox in my wpf application with three items:

<ComboBoxItem Tag="some value">Text</ComboBoxItem>
<ComboBoxItem Tag="some value2">Text2</ComboBoxItem>
<ComboBoxItem Tag="some value3">Text3</ComboBoxItem>


I want to get a selected text or value at runtime. When I do this:

myComboBox.SelectedValue.ToString()


it returns this:

System.Windows.Controls.ComboBoxItem: Text2


How can I get a selected text or value?

Answer

Because you want the Content property of ComboBoxItem you should try like this:

(myComboBox.SelectedValue as ComboBoxItem).Content.ToString();

Or for Tag:

(myComboBox.SelectedValue as ComboBoxItem).Tag.ToString();