What is the simplest way to get the selected text of a combo box containing only text entries?
Solution 1
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()
Solution 2
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!
Solution 3
I just did this.
string SelectedItem = MyComboBox.Text;
Solution 4
If you already know the content of your ComboBoxItem are only going to be strings, just access the content as string:
string text = ((ComboBoxItem)cboPickOne.SelectedItem).Content.ToString();
Solution 5
If you add items in ComboBox as
youComboBox.Items.Add("Data");
Then use this:
youComboBox.SelectedItem;
But if you add items by data binding, use this:
DataRowView vrow = (DataRowView)youComboBox.SelectedItem;
DataRow row = vrow.Row;
MessageBox.Show(row[1].ToString());
DeveloperDan
Updated on January 15, 2021Comments
-
DeveloperDan over 3 years
My WPF ComboBox contains only text entries. The user will select one. What is the simplest way to get the text of the selected ComboBoxItem? Please answer in both C# and Visual Basic. Here is my ComboBox:
<ComboBox Name="cboPickOne"> <ComboBoxItem>This</ComboBoxItem> <ComboBoxItem>should be</ComboBoxItem> <ComboBoxItem>easier!</ComboBoxItem> </ComboBox>
By the way, I know the answer but it wasn't easy to find. I thought I'd post the question to help others. REVISION: I've learned a better answer. By adding SelectedValuePath="Content" as a ComboBox attribute I no longer need the ugly casting code. See Andy's answer below.
-
Can Sahin over 13 yearsIf you know the answer, feel free to post it as well and mark it as the accepted answer. No need for others to do the same research twice. ;-) And who knows, maybe someone can come up with a better solution based on your work...
-
Jim Brissom over 13 yearsAdding to that comment, you could've easily marked your question as community wiki.
-
-
Alex Paven over 13 yearsIt depends though on whether you specify the items explicitly as ComboBoxItems or through bindings directly as strings. In the latter case
.Content
would throw an exception I think. -
DeveloperDan over 13 yearsAs much as I'd like it to be that clean and simple, that alone doesn't work. SelectedValue returns a ComboBoxItem, not the string value I'm looking for. Placing ToString after SelectedValue returns this System.Windows.Controls.ComboBoxItem: followed by the selected text.
-
DeveloperDan over 13 yearsOK. I see you've added SelectedValuePath="Content" as an attribute to the ComboBox. That works! No ugly casting required. It's nice, clean and simple - just what I wanted. Thanks Andy!
-
Sprague over 13 yearsYou can also bind to SelectedValue.Content as the path, this decouples the behavior of ComboBox from the requirement of the binding target.
-
Hille about 5 yearsWhat does
DirectCast(...)
do?