What is the simplest way to get the selected text of a combo box containing only text entries? What is the simplest way to get the selected text of a combo box containing only text entries? wpf wpf

What is the simplest way to get the selected text of a combo box containing only text entries?


In your xml add SelectedValuePath="Content"

<ComboBox   Name="cboPickOne"  SelectedValuePath="Content"  >  <ComboBoxItem>This</ComboBoxItem>  <ComboBoxItem>should be</ComboBoxItem>  <ComboBoxItem>easier!</ComboBoxItem></ComboBox>

This way when you use .SelectedValue.ToString() in the C# code it will just get the string value without all the extra junk:

   stringValue = cboPickOne.SelectedValue.ToString()


Just to clarify Heinzi and Jim Brissom's answers here is the code in Visual Basic:

Dim text As String = DirectCast(cboPickOne.SelectedItem, ComboBoxItem).Content.ToString()

and C#:

string text = ((ComboBoxItem)cboPickOne.SelectedItem).Content.ToString();

Thanks!


I just did this.

string SelectedItem = MyComboBox.Text;