【问题标题】:how do I get the last item from the listview如何从列表视图中获取最后一项
【发布时间】:2012-06-12 07:46:07
【问题描述】:

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

所以我得到了选定的 ListViewItem。

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

如果我通过拖放(例如从 Windows 资源管理器)移动新元素,itemCollection 等于 null,因为我没有在列表视图中选择项目。

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");
        }
}

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

【问题讨论】:

    标签: c# winforms listview drag-and-drop


    【解决方案1】:

    试试这个:

    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
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多