C# 如何从listview中获取最后一项

C# 如何从listview中获取最后一项,c#,winforms,listview,drag-and-drop,C#,Winforms,Listview,Drag And Drop,我的问题是如何处理ListView中的拖放 因此,我得到了所选的ListViewItem ListView.SelectedListViewItemCollection itemCollection = (ListView.SelectedListViewItemCollection)e.Data.GetData("System.Windows.Forms.ListView+SelectedListViewItemCollection"); 如果我通过拖放(例如从windows资源管理器)移动

我的问题是如何处理ListView中的拖放

因此,我得到了所选的ListViewItem

ListView.SelectedListViewItemCollection itemCollection = (ListView.SelectedListViewItemCollection)e.Data.GetData("System.Windows.Forms.ListView+SelectedListViewItemCollection");
如果我通过拖放(例如从windows资源管理器)移动新元素,itemCollection等于null,因为我没有在listview中选择项目

private void DragDropHandler(object sender, DragEventArgs e)
{
        ListView.SelectedListViewItemCollection itemCollection = (ListView.SelectedListViewItemCollection)e.Data.GetData("System.Windows.Forms.ListView+SelectedListViewItemCollection");

        if (itemCollection == null)
        {
            itemCollection = (ListView.SelectedListViewItemCollection)e.Data.GetData("System.Windows.Forms.ListView");
        }
}
在这种情况下,我将获得列表视图中的最后一个元素,如何才能做到这一点?

尝试以下方法:

var r = Enumerable.Empty<ListViewItem>();

if(this.listView1.Items.Count > 0)
    r = this.listView1.Items.OfType<ListViewItem>();

var last = r.LastOrDefault();

if (last != null)
{
    // do your stuff
}
var r=Enumerable.Empty();
如果(this.listView1.Items.Count>0)
r=this.listView1.Items.OfType();
var last=r.LastOrDefault();
if(last!=null)
{
//做你的事
}