33

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.

DeveloperDan
  • 4,626
  • 9
  • 40
  • 65
  • 1
    If 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... – Heinzi Sep 15 '10 at 20:21
  • Adding to that comment, you could've easily marked your question as community wiki. – Jim Brissom Sep 15 '10 at 20:24

7 Answers7

70

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()
SendETHToThisAddress
  • 2,756
  • 7
  • 29
  • 54
Andy
  • 3,631
  • 2
  • 23
  • 32
  • 10
    As 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 Sep 16 '10 at 13:15
  • 7
    OK. 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! – DeveloperDan Sep 16 '10 at 13:24
  • You can also bind to SelectedValue.Content as the path, this decouples the behavior of ComboBox from the requirement of the binding target. – Sprague Oct 26 '10 at 15:56
11

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!

DeveloperDan
  • 4,626
  • 9
  • 40
  • 65
  • 1
    It 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. – Alex Paven Sep 15 '10 at 21:51
10

I just did this.

string SelectedItem = MyComboBox.Text;
6

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();
Jim Brissom
  • 31,821
  • 4
  • 39
  • 33
3

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());
Tarvo Mäesepp
  • 4,477
  • 3
  • 44
  • 92
Mahmoud
  • 60
  • 4
2

Using cboPickOne.Text should give you the string.

mnj
  • 2,539
  • 3
  • 29
  • 58
0
var s = (string)((ComboBoxItem)cboPickOne.SelectedItem).Content;

Dim s = DirectCast(DirectCast(cboPickOne.SelectedItem, ComboBoxItem).Content, String)

Since we know that the content is a string, I prefer a cast over a ToString() method call.

Heinzi
  • 167,459
  • 57
  • 363
  • 519