问题
My problem is about handling drag and drop in a ListView.
So I get the selected ListViewItem.
ListView.SelectedListViewItemCollection itemCollection = (ListView.SelectedListViewItemCollection)e.Data.GetData("System.Windows.Forms.ListView+SelectedListViewItemCollection");
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?
回答1:
Try this:
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
}
来源:https://stackoverflow.com/questions/10945639/how-do-i-get-the-last-item-from-the-listview