Click here to Skip to main content
15,896,153 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Hi, I have a ComboBox in WPF with two ComboBoxItem.

I want to get a different value from each ComboBoxItem that's different from 'Content' property.

In HTML, it's like :
HTML
<select name="test" id="test">
    <option value="valueReturned1">Choice number one displayed</option>
    <option value="valueReturned2">Choice number two displayed</option>
</select>

My XAML is :
XML
<ComboBox x:Name="cbStat">
    <ComboBoxItem Content="item one displayed"></ComboBoxItem>
    <ComboBoxItem Content="item two displayed"></ComboBoxItem>
</ComboBox>


What I have to do for put a 'value' in my ComboBoxItems that i can will get when i select an item ? (like in HTML i get the 'value' property.

I have search for a 'value' property in the ComboBoxItem but not found :(

Thanks by advance for help.
Posted
Comments
[no name] 4-Oct-12 10:42am    
SelectedIndex? What kind of a "value" are you looking for?
Saesee 4-Oct-12 10:44am    
It's exactly what I search. The fact that we obtain the index of the item and not a custom value bothered me; but for a list defined in XAML, it will go.

1 solution

Well, after a little headache... this solution looks good for me.

XAML
XML
<combobox x:name="nameSelectionTypeStat" height="24" xmlns:x="#unknown">
          VerticalAlignment="Center" MinWidth="75">
    <comboboxitem content="Stats. au trimestre"></comboboxitem>
    <comboboxitem content="Stats. à l'année"></comboboxitem>
</combobox>
<Button x:Name="nameValiderTypeStat" Content="Valider le type" Height="24"
        VerticalAlignment="Center" Click="nameValiderTypeStat_Click_1" />

CodeBehind :
C#
private void nameValiderTypeStat_Click_1(object sender, RoutedEventArgs e)
{
    if (nameSelectionTypeStat.SelectedIndex == 0)
    {
        MessageBox.Show("Choice 1");
    }
    else if (nameSelectionTypeStat.SelectedIndex == 1)
    {
        MessageBox.Show("Choice 2");
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900