I can’t get the value of a combobox with WPF

Asked

Viewed 158 times

-3

I can’t get the value of a combobox with WPF. How do I get value? This is the combobox code in XAML.

<ComboBox x:Name="cbxCereais" HorizontalAlignment="Left" Margin="145,178,0,0" VerticalAlignment="Top" Width="220">
    <ComboBoxItem Content="Milho"/>
    <ComboBoxItem Content="Soja"/>
    <ComboBoxItem Content="Feijão"/>
</ComboBox>
  • already? Quick to downvote. Ok it’s wide, I’ll remove a possible answer and leave only one question.

  • It wasn’t me who voted. But I couldn’t understand what problem you’re having.

  • 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.

2 answers

1

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;
  • Content does not solve me, because Content will return Corn and not M, as desired. I have done and this is what returned.

  • And where did you write that you want M to return and not Corn? In the question comments does not help me. Edit the question and write down what you really want to happen.

  • Value and as I said, as I would with web and winform. How do I get value? That’s the tip for this, value.

  • And why posted a code in WPF if you want in winforms and web?

  • Man, read the comments and the posts. I want in WPF and said I would like to do in WPF as today I do with WEB and Winform. This is very lucid. I already found the solution and posted the solution. I think I stop here.

0

Yes, I managed to fix it. I understood that in WPF there is the property Name and not Value, so I could not. See the solution:

ComboBoxItem ComboItem = (ComboBoxItem)cbxCereais.SelectedItem;
            string name = ComboItem.Name;
            lblTeste.Content = name;

And so it is in my XAML:

<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>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.