31

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,318
  • 9
  • 36
  • 63
  • 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

8 Answers8

62

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
  • 1,477
  • 2
  • 15
  • 29
Andy
  • 3,349
  • 2
  • 20
  • 29
  • 9
    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,318
  • 9
  • 36
  • 63
  • 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
7

I just did this.

string SelectedItem = MyComboBox.Text;
3

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
  • 27,745
  • 2
  • 34
  • 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
  • 3,850
  • 2
  • 33
  • 71
Mahmoud
  • 62
  • 4
2

Using cboPickOne.Text should give you the string.

Loreno
  • 1,113
  • 1
  • 11
  • 28
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
  • 151,145
  • 51
  • 326
  • 481
-1

Use the DataRowView.Row.Item[Index] or ItemArray[Index] property to get the SelectedItem, where Index is the index of the column in the DataTable used as itemSource for the combobox. In your case it will be 0. Instead of index you can also pass the Column name also:

VB:

Dim sItem As String=DirectCast(cboPickOne.SelectedItem, DataRowView).Row.Item(1).ToString()

C#

String sItem=((DataRowView)cboPickOne.SelectedItem).Row.Item[1].ToString();

To get the SelectedValue you can use:

VB:

Dim sValue As String=cboPickOne.SelectedValue.ToString()

C#

String sValue=cboPickOne.SelectedValue.ToString();
Simon MᶜKenzie
  • 7,576
  • 13
  • 43
  • 67