Reputation: 870
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?
Upvotes: 1
Views: 3529
Reputation: 921
To get the text/value only you need to do
cbi.Content.ToString() after that line...
Upvotes: 0
Reputation: 17689
you can use
cmbBx1.SelectedItem directly. It will give you the currently Selected Item.
Upvotes: 0
Reputation: 71936
What you want is the contents of the ComboBoxItem.
string month = cbi.Content.ToString();
Upvotes: 2