tags:

views:

151

answers:

3

I have a wpf app with a combobox like this

<ComboBox name="cmbBx1">
  <ComboBoxItem Name=Jan">January</ComboBoxItem>
  <ComboBoxItem Name=Feb">February</ComboBoxItem>
</ComboBox>

In the code behind I need to get the value of the item that was selected to pass to a stored procedure. I can get part of the way by doing this:

ComboBoxItem cbi = (ComboBoxItem)cmbBx1.ItemContainerGenerator.ContainerFromItem(cmbBx1.SelectedItem);

The problem is that the output of that gives me something like System.Windows.Controls.ComboBoxItem: January

I just need the value, i.e. "January" returned. What am I missing?

+2  A: 

What you want is the contents of the ComboBoxItem.

string month = cbi.Content.ToString();
Cameron MacFarland
A: 

you can use

cmbBx1.SelectedItem directly. It will give you the currently Selected Item.

viky
A: 

To get the text/value only you need to do

cbi.Content.ToString() after that line...

Kasumi