Click here to Skip to main content
13,257,667 members (68,132 online)
Rate this:
Please Sign up or sign in to 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 :
<select name="test" id="test">
    <option value="valueReturned1">Choice number one displayed</option>
    <option value="valueReturned2">Choice number two displayed</option>

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

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 4-Oct-12 5:25am
Wes Aday 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

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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

<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>
<Button x:Name="nameValiderTypeStat" Content="Valider le type" Height="24"
        VerticalAlignment="Center" Click="nameValiderTypeStat_Click_1" />

CodeBehind :
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");

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02 | 2.8.171114.1 | Last Updated 4 Oct 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100