当在ListView处于虚拟模式时无法访问所选项目集合? [英] Cannot access the selected items collection when the ListView is in virtual mode?
问题描述
我在虚拟模式一个ListView。我想访问 SelectedItems
属性。
但是当我使用 ListView1.SelectedItems
,我收到以下异常:
无法访问所选项目集合时,在ListView处于虚拟模式
我如何可以访问 ListView1.SelectedItems
在VirtualMode。
我已经通过以下code做了,但它在多个项目中选择一个例外:
的索引超出范围。必须是非负并小于集合的大小。
参数名称:索引的
块引用>列表< ListViewItem的> ListViewItems =新的List< ListViewItem的>();的foreach(在listView1.SelectedIndices INT指数)
{
ListViewItem的SelectedListViewItem = listView1.Items [指数] //例外
ListViewItems.RemoveAt(索引);
}
...无效listView1_RetrieveVirtualItem(对象发件人,RetrieveVirtualItemEventArgs E)
{
e.Item = ListViewItems [e.ItemIndex]
}当你从集合中删除项目(S),总是从最大的指数迭代,以最小的指数,像这样的:
为(INT指数= listView1.SelectedIndices.Count - 1; I> = 0;我 - )
{
...
}这是因为每次你在集合中删除项目时,如果不从最大的迭代,以最小的指数该指数将改变。
I have a ListView in Virtual Mode. I wanna to access
SelectedItems
property.
But when I useListView1.SelectedItems
, I receive the following Exception :Cannot access the selected items collection when the ListView is in virtual mode
How can I access to
ListView1.SelectedItems
in VirtualMode.解决方案I've done it by the following code, but it has an exception when more than one item are selected:
Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
List<ListViewItem> ListViewItems = new List<ListViewItem>(); foreach (int index in listView1.SelectedIndices) { ListViewItem SelectedListViewItem = listView1.Items[index]; // exception ListViewItems.RemoveAt(index); } … void listView1_RetrieveVirtualItem(object sender, RetrieveVirtualItemEventArgs e) { e.Item = ListViewItems[e.ItemIndex]; }
Whenever you remove item(s) from a collection, always iterate from the largest index to the smallest index, like this:
for (int index = listView1.SelectedIndices.Count - 1; i >= 0; i--) { … }
This is because every time you remove an item in a collection, the index will change if you do not iterate from the largest to the smallest index.
这篇关于当在ListView处于虚拟模式时无法访问所选项目集合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!