Click here to Skip to main content
13,256,446 members (54,488 online)
Click here to Skip to main content
Add your own
alternative version


38 bookmarked
Posted 3 Mar 2008

Very Simple WPF Drag and Drop Sample without Win32 Calls

, 16 Mar 2008
Rate this:
Please Sign up or sign in to vote.
Drag and Drop sample without P/Invoke


I learned how to implement drag and drop in WPF from Josh Smith's article Drag and Drop Items in a WPF ListView. Josh's great article and attached code describe many WPF features related to drag and drop functionality. I felt, however, that most of the time people might want to use just a plain, vanilla drag and drop without most of those features. For such people, this article and the related code can be more useful.

On top of that, Josh uses the MouseUtilities class developed by Dan Crevier that calls some Win32 functions. I got around this problem by using the event's GetPosition method. So, one should be able to use the functionality presented here in a Web browser application (XBAP) without any problems.

Later note - added on 03/16/08: Actually this is quite true, one cannot run an XBAP application with DragDrop.DoDragDrop() function under partial trust. To see an example of Drag and Drop implementation for XBAP, take a look at Implementing Drag Drop Operations for Browser Based WPF Applications (XBAP). Also for multiselect drag and drop, see MultiSelect Drag and Drop in WPF.

Using the Code

To use the code, just download it, open the project using Visual Studio 2008, compile and run.

Use drag and drop to re-order the items in the list.

Brief Code Description

There are basically three files containing the code:

  1. Shape.cs
  2. Window1.xaml
  3. Window.xaml.cs

Shape.cs contains the definition for very simple objects that are used as items behind the ListView. Each item has a name and number of sides as properties.

Window1.xaml contains XAML representing a very simple Window with ListView control in it. ListView's ItemsSource property is set to point to a collection of Shape objects.

Window1.xaml.cs contains all the C# plumbing and the functions that actually do drag and drop:

  • ListView1_PreviewMouseLeftButtonDown - originates the drag operation
  • ListView1_Drop - completes the drop operation

Using Event Argument GetPosition Function to Determine Current Mouse Position

Both DragEventArgs and MouseButtonEventArgs classes have the function:

Point GetPosition(IInputElement)

This function returns the current mouse position with respect to the Visual element (passed to the function as the argument). Since DragEventArgs and MouseButtonEventArgs do not have a common superclass or interface that defines the GetPosition function, I wrote the code that takes GetPosition function as a delegate in order to be able to re-use it for both classes. Here is the functionality that returns the index of the current item in the list:

int GetCurrentIndex(GetPositionDelegate getPosition)
    int index = -1;
    for (int i = 0; i < this.ListView1.Items.Count; ++i)
        ListViewItem item = GetListViewItem(i);
        if (this.IsMouseOverTarget(item, getPosition))
            index = i;
    return index;

bool IsMouseOverTarget( Visual target, GetPositionDelegate getPosition)
    Rect bounds = VisualTreeHelper.GetDescendantBounds( target );
    Point mousePos = getPosition((IInputElement) target);
    return bounds.Contains( mousePos );

GetPositionDelegate is defined as:

delegate Point GetPositionDelegate(IInputElement element);

Here is how the function GetCurrentIndex is called: from ListView1_Drop function - responsible for dropping operation (this function uses DragEventArgs):

void ListView1_Drop(object sender, DragEventArgs e)
     int index = this.GetCurrentIndex(e.GetPosition);

From ListView1_PreviewMouseLeftButtonDown function - responsible for starting drag operation (this function uses MouseButtonEventArgs):

void ListView1_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    oldIndex = this.GetCurrentIndex(e.GetPosition);


  • 3rd March, 2008: Initial post
  • 16th March, 2008: Added references to other drag and drop articles


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Nick Polyak
Architect AWebPros
United States United States
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralNice shot Pin
Papa200719-Mar-09 0:36
memberPapa200719-Mar-09 0:36 
Generalmerci Pin
west1881-Feb-09 19:13
memberwest1881-Feb-09 19:13 
GeneralDrag n drop on another UI element Pin
Prashant C18-Aug-08 20:58
memberPrashant C18-Aug-08 20:58 
GeneralRe: Drag n drop on another UI element Pin
Nick Polyak19-Aug-08 16:49
memberNick Polyak19-Aug-08 16:49 
Hey Prashant,
It's been a while since I worked on this article, so some more thorough investigation mihgt be needed to answer your question.
From what I remember, there are following restrictions within this example
1. Drop and PreviewMouseLeftButtonDown events are connected to the List control.
2. The element to drop and the target element are chosen from among list items.

All of this can be changed to work on different controls: e.g. if you have 2 controls between which you want to do drag drop, you can connect Drop and PreviewMouseLeftButtonDown events for some parent panel that holds them both. Choosing the elements to drag and target elements as well as what to do after the drop, will depend on the specifics of the application.

Please rate the article.

Nick Polyak

QuestionRe: Drag n drop on another UI element Pin
Syed Kasim20-May-09 23:54
memberSyed Kasim20-May-09 23:54 
AnswerRe: Drag n drop on another UI element Pin
Nick Polyak22-May-09 6:56
memberNick Polyak22-May-09 6:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.171114.1 | Last Updated 16 Mar 2008
Article Copyright 2008 by Nick Polyak
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid