C# 如何将SelectedListViewItemCollection转换为ListViewItemCollection
我正在尝试编写一个简单的例程来处理ListView中的项目列表,并能够处理所有项目或仅处理选定的项目。我希望这能奏效:C# 如何将SelectedListViewItemCollection转换为ListViewItemCollection,c#,winforms,listview,C#,Winforms,Listview,我正在尝试编写一个简单的例程来处理ListView中的项目列表,并能够处理所有项目或仅处理选定的项目。我希望这能奏效: private void PurgeListOfStudies(ListView.ListViewItemCollection lvic) { /// process items in the list... } 然后这样称呼它: PurgeListOfStudies(myStudiesPageCurrent.ListView.Items); PurgeListOf
private void PurgeListOfStudies(ListView.ListViewItemCollection lvic)
{
/// process items in the list...
}
然后这样称呼它:
PurgeListOfStudies(myStudiesPageCurrent.ListView.Items);
PurgeListOfStudies(myStudiesPageCurrent.ListView.Items.OfType<ListViewItem>());
还是这个
PurgeListOfStudies(myStudiesPageCurrent.ListView.SelectedItems);
但是,这两个列表具有不同且不相关的类型,ListViewItemCollection
和SelectedListViewItemCollection
我已尝试将参数的类型更改为对象
,ICollection
以及其他一些内容。但是,由于类型似乎完全不相关,所以在转换过程中,无论是在编译时还是在运行时,所有操作都会失败
这一切对我来说似乎都很奇怪,因为它们在现实中显然是相同的类型(一个ListViewItem
s的列表)
我在这里遗漏了什么吗?请使用MSDN文档
private void PurgeListOfStudies(IEnumerable items)
{
foreach(MyType currentItem in items) //implicit casting to desired type
{
// process current item in the list...
}
}
使用MSDN文档
private void PurgeListOfStudies(IEnumerable items)
{
foreach(MyType currentItem in items) //implicit casting to desired type
{
// process current item in the list...
}
}
如果您想使PurgeListOfStudies的类型更加安全,可以让它采用
IEnumerable
类型的参数,如下所示:
private void PurgeListOfStudies(IEnumerable<ListViewItem> items)
{
/// process items in the list...
}
然后打电话
PurgeListOfStudies(myStudiesPageCurrent.ListView.ListViewItems());
或
如果您想使PurgeListOfStudies的类型更加安全,可以让它采用
IEnumerable
类型的参数,如下所示:
private void PurgeListOfStudies(IEnumerable<ListViewItem> items)
{
/// process items in the list...
}
然后打电话
PurgeListOfStudies(myStudiesPageCurrent.ListView.ListViewItems());
或
我确实读过MSDN,但忽略了非通用的集合。我尝试了几种通用集合的变体。谢谢。我非常感谢你的回答。我一直试图转换为泛型类型,所以现在我知道了我的错误。我确实读过MSDN,但忽略了非泛型集合。我尝试了几种通用集合的变体。谢谢。我非常感谢你的回答。我一直试图转换为泛型类型,所以现在我知道了我的错误。