5

I have a little problem, I have an array and I want to add that in a Combobox, so I want to use the AddRange method, but it isn't available in WPF, is there a way that I can do it in the combobox?

Thanks.

Sandeep Bansal
  • 6,280
  • 17
  • 84
  • 126

4 Answers4

5

You can't do it in a single statement, no. You will have to loop over the array using foreach, adding each item individually. Obviously you can encapsulate this in a helper or extension method if you plan to do this a lot.

If you're databinding the ComboBox.ItemsSource to an ObservableCollection (rather than manipulating ComboBox.Items directly), there is a trick you can use to avoid getting collection change notifications for each individual Add, described in the answers to this question.

Community
  • 1
  • 1
itowlson
  • 73,686
  • 17
  • 161
  • 157
  • Thanks for some odd reason it didn't click in my head, I used a while loop, going through the entries and then just outputting them. Thanks. – Sandeep Bansal Mar 19 '10 at 01:57
5

You can't but you can use linq to simulate an AddRange

Try write something like that:

    ComboBox    combo;
    String[]    arrOperator = new String[] { "=", "<", "<=", ">", ">=", "<>" };

    combo = new ComboBox();
    arrOperator.ToList().ForEach(item => comboRetVal.Items.Add(item));
BlueWolfy
  • 65
  • 1
  • 3
2

You can try

 comboBox1.ItemsSource = array;
Movingcity
  • 21
  • 3
  • 1
    the OP explicitly states "I want to add" and not set otherwise this would work fine – Firo Oct 26 '12 at 12:17
-5

Try write something like that in codebehind :

comboBox1.Items.AddRange(new[] { "Yellow", "DarkBlue", "Red", "Green" });

or

ArrayList array = new ArrayList();
array.Add("1");
array.Add("2");
comboBox1.Items.AddRange(array);

netmajor
  • 6,507
  • 14
  • 68
  • 100