Instead of adding each item one by one to the ListBox destinationList
from the string array m_List
like this:
而不是将每个项目逐个添加到字符串数组m_List中的ListBox destinationList,如下所示:
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.
我不想将数据绑定到destinationList,因为我想稍后从ListBox中删除一些条目。
4 个解决方案
#1
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.
只需使用(ItemSource as ObservableCollection)...来处理项目,而不是Items.Add等。
#2
6
If you only want to express it more elegantly, then perhaps this will work.
如果你只想更优雅地表达它,那么也许这会起作用。
stringList.ForEach(item => listBox1.Items.Add(item));
#3
0
use OberservableCollection
#4
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)
(为此,我假设你有.Net 4运行时,或者安装了PLinq库)
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.
另外,我没有看到你的foreach循环有什么问题。
#1
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.
只需使用(ItemSource as ObservableCollection)...来处理项目,而不是Items.Add等。
#2
6
If you only want to express it more elegantly, then perhaps this will work.
如果你只想更优雅地表达它,那么也许这会起作用。
stringList.ForEach(item => listBox1.Items.Add(item));
#3
0
use OberservableCollection
#4
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)
(为此,我假设你有.Net 4运行时,或者安装了PLinq库)
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.
另外,我没有看到你的foreach循环有什么问题。