How to use Combo Box AddRange in WPF C #

I have a little problem, I have an array, and I want to add this to Combobox, so I want to use the AddRange method, but it is not available in WPF, is there a way I can do this in the combo box?

Thanks.

+4
source share
4 answers

You cannot do this in one statement, no. You will have to iterate over the array using foreach, adding each element individually. Obviously, you can encapsulate this in an assistant or extension method if you plan on doing this a lot.

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

+5
source

You cannot, but you can use linq to simulate AddRange

Try writing something like this:

ComboBox combo; String[] arrOperator = new String[] { "=", "<", "<=", ">", ">=", "<>" }; combo = new ComboBox(); arrOperator.ToList().ForEach(item => comboRetVal.Items.Add(item)); 
+5
source

You may try

  comboBox1.ItemsSource = array; 
0
source

Try writing something like this 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);

-4
source

Source: https://habr.com/ru/post/1304532/


All Articles