5

Instead of adding each item one by one to the ListBox destinationList from the string array m_List like this:

foreach (object name in m_List)
{
    destinationList.Items.Add((string)name);
}

Is there any better way I can do it?

I don't want to bind the data to the destinationList since I want to delete some entries from the ListBox later on.

5
  • 3
    apart from the string casting, i don't find anything bad in the above code if binding is not your option. But i would recommend you to bind the listbox to your collection(ObservableCollection would be better). Even if there is something better than this, it would iterate ultimately through your collection. If you're going to use it frequently, you can write a method for that, sending in your list as the parameter. Commented Apr 29, 2010 at 11:58
  • I don't want to bind it since I want to remove few items from it later on. Commented Apr 29, 2010 at 12:00
  • Why do you think you can't remove items if they are bound? Commented Apr 29, 2010 at 12:03
  • Actually I tried using ItemsSource. I have a treeview with checkboxes whose selected items would go into the destinationList. So on checked it should be added and on unchecked it should be removed. so if i remove all and again try to add by clicking on head node it shows error as Operation is not valid while ItemsSource is in use. Access and modify elements with ItemsControl.ItemsSource instead. Commented Apr 29, 2010 at 12:09
  • For this, you can add/remove items or clear the entire ObservableCollection bound to the ListBox instead of using array. Commented Apr 29, 2010 at 12:15

4 Answers 4

7

If you only want to express it more elegantly, then perhaps this will work.

stringList.ForEach(item => listBox1.Items.Add(item));
Sign up to request clarification or add additional context in comments.

Comments

5

HTH:

    string[] list = new string[] { "1", "2", "3" };

    ObservableCollection<string> oList;
    oList = new System.Collections.ObjectModel.ObservableCollection<string>(list);
    listBox1.DataContext = oList;

    Binding binding = new Binding();
    listBox1.SetBinding(ListBox.ItemsSourceProperty, binding);

    (listBox1.ItemsSource as ObservableCollection<string>).RemoveAt(0);

Just use (ItemSource as ObservableCollection)... to work with items, and not Items.Add etc.

3 Comments

It gives error since if the parent checkbox is checked all the child checkboxes' checked event is called and it throws same error. Instead of adding a single item as Items.Add what can be done?
Sorry, but i don't understand you, parent checkbox? Can you post some code, or describe your problem with more details?
If you bind listview to source, you must work with source, and not with the listviewitems. As for you problem, i have to see code, which throw exception. This kind of exception usually happend if you work not with the source but with items instead. And as for the checkbox logic it's up to you (if it's in you code), you can change check behavior (i'm talking about treeview) if you don't like it.
0

Okay.. if binding is not an option - and I would probably go that way if it was... then the only more efficient way to populate the listbox would be to do it in parallel.

(For this to work I am assuming you have the .Net 4 runtime, or the PLinq libraries installed)

The following code would show massive improvements on a multicore machine provided the collection of data was large enough to warrant the overhead of the initial setup. So this would only be viable for larger arrays.

Parallel.ForEach(list, r => destinationList.Items.Add(r));

Else I don't see anything wrong with your foreach loop.

Comments

-1

use OberservableCollection

2 Comments

how can i use it? you mean instead of string array?
@Archie: You'll have to copy your array contents into ObservableCollection and then bind the OC to the ListBox, if you want to preserve the original contents.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.