Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: WPF C#4.0 combobox .NET4
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>
</select>
My XAML is :
<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 Frown | :(
 
Thanks by advance for help.
Posted 4-Oct-12 5:25am
Saesee176
Comments
Wes Aday at 4-Oct-12 10:42am
   
SelectedIndex? What kind of a "value" are you looking for?
Saesee at 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
good
Please Sign up or sign in to vote.

Solution 1

Well, after a little headache... this solution looks good for me.
 
XAML
<combobox x:name="nameSelectionTypeStat" height="24" xmlns:x="#unknown">
          VerticalAlignment="Center" MinWidth="75">
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");
    }
}
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 240
1 CHill60 210
2 Peter Leow 170
3 Richard Deeming 166
4 Richard MacCutchan 159
0 Sergey Alexandrovich Kryukov 9,248
1 OriginalGriff 6,806
2 Peter Leow 4,432
3 Zoltán Zörgő 3,919
4 Richard MacCutchan 2,782


Advertise | Privacy | Mobile
Web01 | 2.8.150129.1 | Last Updated 4 Oct 2012
Copyright © CodeProject, 1999-2015
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