Home > DeveloperSection > Forums > Add items to ComboBox at runtime?
Royce Roy
Royce Roy

Total Post:134

Points:938
Posted on    April-10-2014 11:51 PM

 C# C# 
Ratings:


 1 Reply(s)
 1459  View(s)
Rate this:

I am trying to add items to a ComboBox (say Name="labelComboBox") at runtime when I pressed an add button (say with Name="add2labels" Click="add2labels_Click"). But the ComboBox cannot show the values I newly added. What did I miss?

The following is the event handler for the add button:

private List<String> labels = new List<String>();

... ...

private void add2labels_Click(object sender, RoutedEventArgs e)

{

    labels.Add("new value");

    labelComboBox.ItemsSource = labels;

}

P.S. I am pretty sure the values were added to List<String> labels correctly (its count did increase each time).



Pravesh Singh

Total Post:411

Points:2881
Posted on    April-11-2014 12:11 AM

Hi Royce,

Combobox has a display and value member to add values to combo-box you need to specify both.

Try this

ComboboxItem item = new ComboboxItem();

item.Text = "new value";

item.Value = 12;

labels.Items.Add(item);


Don't want to miss updates? Please click the below button!

Follow MindStick