Drag and Drop in WPF
来源:互联网 发布:慢跑减肥 知乎 编辑:程序博客网 时间:2024/05/22 09:39
Introduction
Drag&Drop can drasticly improve the productiviy and user experience of a software. But only a few programmers provide drag and drop functionality in their applications, because they think its much more dificult than it really is. This article shows how simple drag and drop can be implemented in WPF.
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 callingGetDataPresent() on the event args. If the data can be dropped, set theEffect 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 theGetData() method on the Data object provided in the event args.
...and that's all the magic.
Drag
To start the drag operation, we have to detect a mouse move while the left mouse button is pressed. To do this we have to hook up handlers on thePreviewMouseMove
and PreviewMouseLeftButtonDown
events.
To prevent occasionally drags, its a good design to not start the drag operation until the user has moved the mouse cursor by a couple of pixels. WPF provides a constant that contains the amount of pixel that Windows uses.
When the drag is initiated, we need to specify the data we want to drag. In our case its the data of theListViewItem
we dragged. We find the ListViewItem
in theOriginalSource
of the mouse event args. By calling ItemContainerGenerator.ItemFromContainer
we get the data behind theListViewItem
.
Create a DataObject
to transport the data to the drop location. The constructor takes two arguments. A string that describes the format and the data we want to drag.
<ListView x:Name="DragList" 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 VisualTreeprivate 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
To make an element be a drop location, set the AllowDrop property to true. When the user drags an item over the element, theDragEnter event is called. In this event you can analyze the data and decide if a drop is allowed or not.
When the user releases the mouse button the Drop event is called. The data is available in theDataObject provided in the DragEventArgs.
<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); }}
Source reference: 点击打开链接
- Drag and Drop in WPF
- Drag and Drop Items in a WPF ListView
- Drag And Drop In Javascript
- Drag and drop in unity
- Basic drag and drop in WinForms
- Simple Drag and Drop in ExtJS
- Very Simple WPF Drag and Drop Sample without Win32 Calls
- Drag and Drop
- 学习drag and drop
- 关于drag and drop
- html5 Drag and drop
- Ajax Drag and Drop
- QT5 drag and drop
- HTML5 Drag and Drop
- drag and drop学习
- android - Drag and Drop
- html5 Drag and Drop
- Qt Drag and Drop
- shell 字符串操作小结
- 杭电acm2019数列有序
- 27-Dialog练习题,点击弹出一个对话框,选项是一个列表
- spring+jta实现全局事物管理
- 豆瓣出错:java.lang.NoClassDefFoundError: com.google.gdata.client.douban.DoubanService
- Drag and Drop in WPF
- 复述抽取生成doc04-实现02-对给定句子搜索相关的句子
- 关于求FIRST集和FOLLOW集
- word2010的设置记录
- Ubuntu 11.10 打开 windows文件乱码解决方法
- 豆瓣出错Error generating final archive:Found duplicate file for APK:net/oauth/consumer.properties.sample
- 54. 长命的临时解决方案
- hdu1241(最简单的搜索)
- vim gdb 调试及gcc优化