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

  1. Detect a drag as a combinatination of MouseMove and MouseLeftButtonDown
  2. Find the data you want to drag and create a DataObject that contains the format, the data and the allowed effects.
  3. Initiate the dragging by calling DoDragDrop()
  4. Set the AllowDrop property to True on the elements you want to allow dropping.
  5. 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.
  6. 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: 点击打开链接



原创粉丝点击