将图像从一个列表视图拖动到另一个列表视图 [英] drag image from one listview to another listview

查看:94
本文介绍了将图像从一个列表视图拖动到另一个列表视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将图像从一个列表视图拖动到wpf中的另一个列表视图

how to drag image from one listview to another listview in wpf

推荐答案

拖放6个步骤



检测拖动作为MouseMove和MouseLeftButtonDown的组合

找到要拖动的数据并创建包含格式,数据和允许效果的DataObject。

通过调用DoDragDrop启动拖动()

在要允许删除的元素上将AllowDrop属性设置为True。

向DragEnter事件注册处理程序检测拖放位置上的拖动。通过在事件args上调用GetDataPresent()来检查格式和数据。如果可以删除数据,请在事件参数上设置Effect属性以显示相应的鼠标光标。

当用户释放鼠标按钮时,将调用DragDrop事件。通过在事件args中提供的Data对象上调用GetData()方法来获取数据。



拖动:



< listview x:name =DragListxmlns:x =#未知> PreviewMouseLeftButtonDown =List_PreviewMouseLeftButtonDown

PreviewMouseMove =List_MouseMove/>









private void List_PreviewMouseLeftButtonDown(object sender,MouseButtonEventArgs e)

{

//存储鼠标位置

startPoint = e.GetPosition(null);

}









private void List_MouseMove(对象发送者,MouseEventArgs e)

{

//获取当前鼠标位置

Point mousePos = e.GetPosition(null);

Vector diff = startPoint - mousePos;



if(e.LeftButton == MouseButtonState.Pressed&&

Math.Abs​​(diff.X)> SystemParameters.MinimumHorizo​​ntalDragDistance ||

Math.Abs​​(diff.Y)> SystemParameters。 MinimumVerticalDragDistance)

{

//获取拖动的ListVi ewItem

ListView listView =发送者作为ListView;

ListViewItem listViewItem =

FindAnchestor< listviewitem>((DependencyObject)e.OriginalSource);



//查找ListViewItem背后的数据

联系联系人=(联系人)listView.ItemContainerGenerator。

ItemFromContainer( listViewItem);



//初始化拖动& drop操作

DataObject dragData = new DataObject(myFormat,contact);

DragDrop.DoDragDrop(listViewItem,dragData,DragDropEffects.Move);

}

}









//帮助搜索VisualTree

私有静态T FindAnchestor< t>(DependencyObject current)

其中T:DependencyObject

{

do

{

if(当前为T)

{

返回(T)当前;

}

current = VisualTreeHelper.GetParent(当前);

}

而( current!= null);

返回null;

}



掉落:

< listview x:name =DropList> Drop =DropList_Drop

DragEnter =DropList_DragEnter

AllowDrop =True/>









private void DropList_DragEnter(object sender,DragEventArgs e)

{

if(!e.Data.GetDataPresent(myFormat)||

sender == e.Source)

{

e。效果= DragDropEffects.None;

}

}









private void DropList_Drop(object sender,DragEventArgs e)

{

if(e.Data.GetDataPresent( myFormat))

{

联系联系人= e.Data.GetData(myFormat)作为联系人;

ListView listView = sender as ListView;

listView.Items.Add(联系方式);

}

}
Drag&Drop in 6 Steps

Detect a drag as a combinatination of MouseMove and MouseLeftButtonDown
Find the data you want to drag and create a DataObject that contains the format, the data and the allowed effects.
Initiate the dragging by calling DoDragDrop()
Set the AllowDrop property to True on the elements you want to allow dropping.
Register a handler to the DragEnter event to detect a dragging over the drop location. Check the format and the data by calling GetDataPresent() on the event args. If the data can be dropped, set the Effect property on the event args to display the appropriate mouse cursor.
When the user releases the mouse button the DragDrop event is called. Get the data by calling the GetData() method on the Data object provided in the event args.

Drag:

<listview x:name="DragList" xmlns:x="#unknown"> PreviewMouseLeftButtonDown="List_PreviewMouseLeftButtonDown"
PreviewMouseMove="List_MouseMove"/>




private void List_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
// Store the mouse position
startPoint = e.GetPosition(null);
}




private void List_MouseMove(object sender, MouseEventArgs e)
{
// Get the current mouse position
Point mousePos = e.GetPosition(null);
Vector diff = startPoint - mousePos;

if (e.LeftButton == MouseButtonState.Pressed &&
Math.Abs(diff.X) > SystemParameters.MinimumHorizontalDragDistance ||
Math.Abs(diff.Y) > SystemParameters.MinimumVerticalDragDistance )
{
// Get the dragged ListViewItem
ListView listView = sender as ListView;
ListViewItem listViewItem =
FindAnchestor<listviewitem>((DependencyObject)e.OriginalSource);

// Find the data behind the ListViewItem
Contact contact = (Contact)listView.ItemContainerGenerator.
ItemFromContainer(listViewItem);

// Initialize the drag & drop operation
DataObject dragData = new DataObject("myFormat", contact );
DragDrop.DoDragDrop(listViewItem, dragData, DragDropEffects.Move);
}
}




// Helper to search up the VisualTree
private static T FindAnchestor<t>(DependencyObject current)
where T : DependencyObject
{
do
{
if( current is T )
{
return (T)current;
}
current = VisualTreeHelper.GetParent(current);
}
while (current != null);
return null;
}

Drop:
<listview x:name="DropList"> Drop="DropList_Drop"
DragEnter="DropList_DragEnter"
AllowDrop="True" />




private void DropList_DragEnter(object sender, DragEventArgs e)
{
if (!e.Data.GetDataPresent("myFormat") ||
sender == e.Source)
{
e.Effects = DragDropEffects.None;
}
}




private void DropList_Drop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent("myFormat"))
{
Contact contact = e.Data.GetData("myFormat") as Contact;
ListView listView = sender as ListView;
listView.Items.Add(contact);
}
}


这篇关于将图像从一个列表视图拖动到另一个列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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