Drag & Drop in WPF

Drag & Drop can greatly improve the usability of software. Yet only a few developers add this functionality to their applications, because they think it's harder than it really is. This article shows how easy it is to implement Drag & Drop in WPF. Drag & Drop in 6 steps:

  1. detect the start of a drag with the MouseMove and MouseLeftButtonDown events;
  2. determine the data you want to "drag" and create a DataObject that holds the format and the data;
  3. start the drag by calling DoDragDrop();
  4. set AllowDrop=true on the elements that will accept the data;
  5. add a DragEnter event handler to detect that something is being "dragged" over the "drop target". If the data can be accepted, set the Effect property of the event handler's argument;
  6. when the user releases the mouse button, the Drop event fires. You can get the data with the e.Data.GetData() method

That's all the magic there is.

Only registered users can leave a comment.

Sign in or register to leave a comment.