Fill WPF List with String Array

Instead of adding each item one by one to the ListBox destinationListfrom an array of strings m_List, like this:

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

Is there a better way to do this?

I do not want to bind data to the mailing list, since later I want to delete some entries from the list.

+3
source share
4 answers

NTN:

    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, not Items.Add, etc.

+5
source

If you want to express it more elegantly, it might work.

stringList.ForEach(item => listBox1.Items.Add(item));
+5
source

OberservableCollection

0

.. - , , , ... .

( , ​​ .Net 4 PLinq)

, , . , .

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

foreach.

0

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


All Articles