列表视图拖放,如何检查是否将项目放在同一个列表视图中
本文关键字:视图 列表 项目 同一个 是否 检查 拖放 何检查 | 更新日期: 2023-09-27 18:32:47
我有两个列表视图[listView1,listLocal],它们是本地PC和远程PC
的两个文件浏览器如果我将项目拖放到我从中拖动它们的同一列表视图中,它应该复制/移动。否则,如果我将项目放入其他列表视图中,那么它应该发送/接收。
所以 let listLocal 是本地 PC 的文件资源管理器,listView1 是远程 PC 的文件资源管理器。
所以我需要一个条件来检查我是否将拖动的项目拖放到另一个列表视图中。
private void listLocal_ItemDrag(object sender, ItemDragEventArgs e)
{
if (_currAddress == null) return; //if the current address is My Computer
_DraggedItems.Clear();
foreach (ListViewItem item in listLocal.SelectedItems)
{
_DraggedItems.Add((ListViewItem)item.Clone());
}
// if ( some condition ) call the same listview **listLocal.DoDragDrop**
listLocal.DoDragDrop(e.Item, DragDropEffects.All);
// else call the other listview
listView1.DoDragDrop(e.Item, DragDropEffects.All);
}
此外,当列表视图拖放触发时,我需要检查拖动的项目是否来自同一列表视图的条件。
private void listView1_DragDrop(object sender, DragEventArgs e)
{
Point p = listView1.PointToClient(MousePosition);
ListViewItem targetItem = listView1.GetItemAt(p.X, p.Y);
// if ( some condition )
//here i need to check if the dragged item came from the same listview or not
{
if (targetItem == null)
{
PreSend(currAddress); //send to the current address
}
else
{
PreSend(targetItem.ToolTipText); //send to the target folder
}
return;
}
//otherwise
if (targetItem == null) { return; } //if dropped in the same folder return
if ((e.Effect & DragDropEffects.Move) == DragDropEffects.Move)
{
Thread thMove = new Thread(unused => PasteFromMove(targetItem.ToolTipText, DraggedItems));
thMove.Start();
}
else
{
Thread thCopy = new Thread(unused => PasteFromCopy(targetItem.ToolTipText, DraggedItems));
thCopy.Start();
}
}
您需要实现 DragEnter 事件来验证是否正在拖动正确的对象。 可以使用 ListViewItem.ListView 属性来验证它是否来自另一个 ListView。 喜欢这个:
private void listView1_DragEnter(object sender, DragEventArgs e) {
// It has to be a ListViewItem
if (!e.Data.GetDataPresent(typeof(ListViewItem))) return;
var item = (ListViewItem)e.Data.GetData(typeof(ListViewItem));
// But not one from the same ListView
if (item.ListView == listView1) return;
// All's well, allow the drop
e.Effect = DragDropEffects.Copy;
}
无需在 DragDrop 事件处理程序中进行进一步检查。