none
combobox wpf add items

    Question

  • I want to add items to a combobox dynamically - thus depending on user input. I have tried using:

    Code Snippet

    comboBox1.Items.Add("E");



    but nothing happens, the combobox does not add the items as it should. Is the function that I am using only available to windows forms and not to wpf? How can I add items to a wpf combobox?
    Friday, March 07, 2008 4:13 PM

Answers

  • This code should work.  After adding an item if you click the ComboBox to expand the items list do you see your addition here?  I'm guessing that the item is getting added, but you aren't setting the SelectedIndex property to show it in the ComboBox.  Try this:

    Code Snippet

    comboBox1.Items.Add("E");

    comboBox1.SelectedIndex = comboBox1.Items.Count - 1;

     

    If you immediately set the SelectedIndex property to Items.Count -1 after adding an item to your ComboBox, it'll appear in the ComboBox as the selected item as you previously expected.
    Friday, March 07, 2008 4:52 PM

All replies

  • This code should work.  After adding an item if you click the ComboBox to expand the items list do you see your addition here?  I'm guessing that the item is getting added, but you aren't setting the SelectedIndex property to show it in the ComboBox.  Try this:

    Code Snippet

    comboBox1.Items.Add("E");

    comboBox1.SelectedIndex = comboBox1.Items.Count - 1;

     

    If you immediately set the SelectedIndex property to Items.Count -1 after adding an item to your ComboBox, it'll appear in the ComboBox as the selected item as you previously expected.
    Friday, March 07, 2008 4:52 PM
  • thank you very much!
    Friday, March 07, 2008 4:53 PM