I am trying to create and add some ComboBoxItems to a ComboBox.
Code |
The problem is that the ComboBox displays a lot more info than I'd like to:

How can I make it display only the 'Personal Computer' part?
I know there are other ways to add items to a ComboBox but I need every ComboBoxItem to store two values: a string value to show in the ComboBox and an int value which I store using the Tag property. I need this int value for further use so unfortunately ignoring it is not an option.
So... is there any way to properly display the ComboBox items?

Subhendu DeRank #1,108 · 1.7K reputationPosted Dec 9, 2010, 6:21 AM EST
Daniel Vlasceanu5 reputationPosted Dec 9, 2010, 8:15 AM EST
This is ridiculous. There are explicit ways to 'manually' create, customize and add ComboBoxItem objects to ComboBoxes but you can't use them? It works just fine in WPF/C# projects... What am I missing?
CrishRank #1,487 · 1.1K reputationPosted Dec 10, 2010, 12:37 AM EST
you can refer this code to add item in combobox.
comboBox1.Items.Add(new Item("Blue", 1));
comboBox1.Items.Add(new Item("Red", 2));
comboBox1.Items.Add(new Item("Nobugz", 666));
Daniel Vlasceanu5 reputationPosted Dec 10, 2010, 2:22 AM EST
I finally managed to get what I wanted.
foreach (var dt in deviceTypes) { devicesComboBox.Items.Add(new MyClass{ TypeName = dt, ID = d.ID }); } devicesComboBox.DisplayMember = "TypeName"; int id = ((MyClass) devicesCombobox.SelectedItem).ID; ... public class MyClass { public string TypeName { get; set;} public int ID { get ; set; } }