ListView 处于虚拟模式时无法访问所选项目集合?
我有一个处于虚拟模式的 ListView。我想访问 SelectedItems
属性。
但是当我使用 ListView1.SelectedItems
时,我收到以下异常:
Cannot access the selected items collection when the ListView is in virtual mode
如何在 VirtualMode 中访问 ListView1.SelectedItems
。
I have a ListView in Virtual Mode. I wanna to access SelectedItems
property.
But when I use ListView1.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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是很旧的帖子,但也许其他人会受益。
只需使用ListView.SelectedIndexCollection col = listView.SelectedIndices;
然后您可以访问一个项目:
..但是您将无法使用 foreach 迭代 ListView.Items,因为在此模式下没有可用的迭代器。使用索引器运行得很好:-)
当尝试使用 foreach 时,你会得到一个异常:
It is quite old post but maybe someone else will benefit.
Simply use
ListView.SelectedIndexCollection col = listView.SelectedIndices;
Then you can access an item:
..but you won't be able to iterate through ListView.Items using foreach because there is no iterator available in this mode. Using indexer is just flying fine :-)
When trying to use foreach you get an exception:
来自 docs
在虚拟模式下, Items 集合被禁用。尝试访问它会导致 InvalidOperationException。 CheckedItems 集合和 SelectedItems 集合也是如此。如果要检索选定或选中的项目,请改用 SelectedIndices 和 CheckedIndices 集合。
From the docs
In virtual mode, the Items collection is disabled. Attempting to access it results in an InvalidOperationException. The same is true of the CheckedItems collection and the SelectedItems collection. If you want to retrieve the selected or checked items, use the SelectedIndices and CheckedIndices collections instead.
我将所有项目存储在列表中,并使用此列表在 RetrieveVirtualItem 中提供项目
您可以找到如下所示的选定项目
I you store all items in list and use this list to give item in RetrieveVirtualItem
you can find selected items like following
我已经通过以下代码完成了此操作,但是当选择多个项目时会出现异常:
每当从集合中删除项目时,始终从最大索引迭代到最小索引,如下所示:
这是因为每次删除集合中的项目时,索引都会改变如果不从最大索引到最小索引进行迭代。
I've done it by the following code, but it has an exception when more than one item are selected:
Whenever you remove item(s) from a collection, always iterate from the largest index to the smallest index, like this:
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.