当 ListView 处于虚拟模式时,如何枚举它?
当ListView处于虚拟模式时如何枚举它?
我正在使用 OfType<>()
方法来枚举列表视图项。但它会抛出异常,例如列表视图处于虚拟模式时无法枚举。
这是我的代码
List<String> lst= myListView.Items.OfType<ListViewItem>().Select(X=>X.Text).ToList();
那么当 ListView 处于虚拟模式时,如何从 ListView 中获取项目?
请给我发布一种使用 .OfType<> 的方法
提前致谢
How to enumerate a ListView when it is in virtual mode ?
I'm using OfType<>()
method to enumerate the list view items. But its throwing an exception like, the list view can not be enumerated when it is in virtual mode.
Here is my code
List<String> lst= myListView.Items.OfType<ListViewItem>().Select(X=>X.Text).ToList();
So how do i get the item from the ListView when it is in Virtual mode?
Please post me a way to use .OfType<>
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当处于虚拟模式时,您无法将项目从列表视图中取出,因为这些项目不在列表视图中。这就是虚拟模式的全部意义。
相反,您按住项目,列表视图会询问显示项目所需的信息。如果您在虚拟模式下成功操作列表视图,那么几乎可以肯定您已经在列表中的某个位置有了这些项目。
引用文档:
When it's in virtual mode you can't get the items out of the list view because the items aren't in the list view. That's the whole point of virtual mode.
Instead, you hold the items and the list view asks for the information it needs to display the items. If you are operating a list view successfully in virtual mode then you almost certainly already have the items in a list somewhere.
Quoting from the documentation:
如果在列表处于虚拟模式时调用
ListViewItemCollection.GetEnumerator()
方法,则会引发异常。这意味着您无法通过任何 LINQ 方法(包括OfType()
)访问它们。但是,您可以对列表进行简单的迭代:
但是(再次),如果列表是虚拟的,它可能有大量项目,因此这可能需要一段时间:)
The
ListViewItemCollection.GetEnumerator()
method throws an exception if it is called when the list is in virtual mode. This means it will be impossible for you to access them via any LINQ method, includingOfType()
.However, you can just do a simple iteration of the list:
However (again), if the list is virtual, it probably has a large number of items, so this could take a while :)