It is necessary to make a cast for a ComboBoxItem
.
var item = cbxCereais.SelectedValue as ComboBoxItem;
var text = item.Content.ToString();
This occurs because the items of a ComboBox
are treated as objects of the ComboBoxItem
. Making the cast as informed and accessing the property Content
, solves the problem.
Searching for the Name/Value property
Complementing the reply after having understood what was requested after comments made.
To fetch the "value" of a Comboboxitem, we can use the property Name:
<ComboBox x:Name="cbxCereais" HorizontalAlignment="Left" Margin="145,178,0,0" VerticalAlignment="Top" Width="220">
<ComboBoxItem Content="Milho" Name="M" />
<ComboBoxItem Content="Soja" Name="S" />
<ComboBoxItem Content="Feijão" Name="F" />
</ComboBox>
And in the code:
var item = cbxCereais.SelectedValue as ComboBoxItem;
var text = item.Name;
already? Quick to downvote. Ok it’s wide, I’ll remove a possible answer and leave only one question.
– pnet
It wasn’t me who voted. But I couldn’t understand what problem you’re having.
– Jéf Bueno
the question was wide indeed and I edited. Dude, I looked on the internet and did not find. What I want is, I have items in my combobox, like, Corn, Soy, Beans, Wheat and each item of this should have a value and be able to take this value and pass as parameter. I can get the content, but not the value, because I couldn’t define it.
– pnet