Alan Coromano Alan Coromano - 1 year ago 62
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:


it returns this:

System.Windows.Controls.ComboBoxItem: Text2

How can I get a selected text or value?

Answer Source

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();