如何从列表视图得到最后一个项目 [英] how do I get the last item from the listview

查看:125
本文介绍了如何从列表视图得到最后一个项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是关于在ListView处理拖放。

My problem is about handling drag and drop in a ListView.

所以我得到的选择ListViewItem的。

So I get the selected ListViewItem.

ListView.SelectedListViewItemCollection itemCollection = (ListView.SelectedListViewItemCollection)e.Data.GetData("System.Windows.Forms.ListView+SelectedListViewItemCollection");

如果我移动通过拖放放一个新的元素;降(例如,从Windows资源管理器)中,itemCollection等于空,因为我不选择在列表视图中的项目

If i move a new element via drag&drop (for example from windows explorer), the itemCollection equals null, because i dont select an item in the 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");
        }
}

对于这种情况我会得到在列表视图中的最后一个元素,我该怎么办呢?

for this case I would get the last element in list view, how can i do that?

推荐答案

试试这个:

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
}

这篇关于如何从列表视图得到最后一个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆