开发者

How to use Combo Box AddRange in WPF C#

开发者 https://www.devze.com 2022-12-24 12:12 出处:网络
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 wa开发者_如何学Pythony that I can do it

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 wa开发者_如何学Pythony that I can do it in the combobox?

Thanks.


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.


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));


You can try

 comboBox1.ItemsSource = array;


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);

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号