Click here to Skip to main content
15,881,898 members
Articles / Desktop Programming / WPF

MultiSelect Drag and Drop in WPF

Rate me:
Please Sign up or sign in to vote.
3.33/5 (14 votes)
6 Mar 2008CPOL2 min read 90.3K   2.2K   26   11
Discusses implementation of Drag and Drop functionality for multiselected items in ListBox/ListView

WPF_MultiSelect_DragDrop/multiselectimage2.jpg

Note - this is one of my earliest articles and I cannot guarantee that the sample is working and the presentation is clear. I plan to write another article that will incorporate the samples from my earliest articles making them work. Thanks for your understanding. 

Introduction

This article is a variation on the previous theme: Drag and Drop (see Very simple WPF Drag and Drop Sample without Win32 calls). Here, drag and drop of multiple items selected within WPF ListView or ListBox is discussed.

Using the Code

To run the sample, open it in Visual Studio 2008. Then simply compile and run the application.

Several points on using the sample:

  • You can select multiple entries in the list by using Ctrl or Shift key together with the mouse.
  • In order to initiate the drag operation, you have to click on one of the selected items one more time and move the mouse while holding it down.
  • If at the end of "Drag", the mouse pointer will be over one of the selected items, no operation will be performed.
  • The selected items do not have to be contiguous at the start of the drag operation, but after they are dropped they become contiguous.
  • The order of the dragged and drop items remains the same after the drop.

Code Description

Here are some code excerpts. In function ListView1_PreviewMouseLeftButtonDown called in the beginning of the drag operation, we create a set of selected items (Dictionary with null values) and pass it to the DragDrop.DoDragDrop(...) function as data item:

C#
Dictionary shapes = new Dictionary();

if (ListView1.SelectedItems.Count == 0)
    return;

foreach(Shape shape in ListView1.SelectedItems)
{
    shapes[shape] = null;
}

Shape currentShape = ListView1.Items[index] as Shape;

// we do not initiate drag if the mouse descended on
// a non-selected item during the beginning of drag
if (!shapes.ContainsKey(currentShape))
    return;

DragDrop.DoDragDrop(this.ListView1, shapes, allowedEffects);

Function ListView1_Drop (the one implementing the drop operation) is slightly more complicated. First we record the list item into which the selected items are dropped:

C#
int index = this.GetCurrentIndex(e.GetPosition);
...
Shape dropTargetShape = myShapes[index];

Then we build a list of selected items to be dropped:

C#
List dropList = new List();
foreach(Shape shape in myShapes)
{
    if (!selectedShapes.ContainsKey(shape))
        continue;

    dropList.Add(shape);
}

We need this step in order to ensure that the dropped items are in the same order as they were originally. (In ListView.SelectedItems collection, the items are stored in the order in which they are selected, not in the order in which they are in the ListView).

Then we remove all the selected items from the collection myShapes (which is the collection of ListView items):

C#
foreach(Shape shape in dropList)
{
    myShapes.Remove(shape);
}

Then we get the (possibly) new index of the drop target item within the modified collection:

C#
// find index of the drop target item after the removal
// of the items to be dropped
int selectIndex = myShapes.IndexOf(dropTargetShape);

Finally we insert the items into the collection before the drop target item:

C#
for(int i = 0; i < dropList.Count; i++)
{
    Shape shape = dropList[i];
    myShapes.Insert(i + selectIndex, shape);
    ...
}

History

  • 5th March, 2008: Initial post
  • 6th March, 2008: Added a screen shot

License

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


Written By
Architect AWebPros
United States United States
I am a software architect and a developer with great passion for new engineering solutions and finding and applying design patterns.

I am passionate about learning new ways of building software and sharing my knowledge with others.

I worked with many various languages including C#, Java and C++.

I fell in love with WPF (and later Silverlight) at first sight. After Microsoft killed Silverlight, I was distraught until I found Avalonia - a great multiplatform package for building UI on Windows, Linux, Mac as well as within browsers (using WASM) and for mobile platforms.

I have my Ph.D. from RPI.

here is my linkedin profile

Comments and Discussions

 
Suggestion[My vote of 1] Sad... Such promise. Pin
Destiny77720-Jun-18 12:15
Destiny77720-Jun-18 12:15 
GeneralRe: [My vote of 1] Sad... Such promise. Pin
Nick Polyak21-Jun-18 7:08
mvaNick Polyak21-Jun-18 7:08 
SuggestionVerbatim Code Pin
Yoda Bytes21-Jul-14 4:37
Yoda Bytes21-Jul-14 4:37 
GeneralMy vote of 3 Pin
Luck)30-Nov-10 18:57
Luck)30-Nov-10 18:57 
GeneralMy vote of 1 Pin
Ma3ztro7-May-09 4:17
Ma3ztro7-May-09 4:17 
GeneralMy vote of 1 Pin
zbend30-Jan-09 11:42
zbend30-Jan-09 11:42 
QuestionBug? Pin
cherishnews18-Jan-09 14:05
cherishnews18-Jan-09 14:05 
Hi,

Thank you for the great article.
It serves as good reference for me since I am going to implement multiselect D&D in listbox and listview soon.
However, I'm not sure what is wrong, but when I ran your program, selected item/s will never go to the last index of the listbox, though it was drag to the last index.
AnswerRe: Bug? Pin
Nick Polyak18-Jan-09 14:24
mvaNick Polyak18-Jan-09 14:24 
AnswerRe: Bug? Pin
Nick Polyak18-Jan-09 14:27
mvaNick Polyak18-Jan-09 14:27 
QuestionWhere's screens? Pin
dethtroll5-Mar-08 23:36
dethtroll5-Mar-08 23:36 
GeneralRe: Where's screens? Pin
Nick Polyak6-Mar-08 15:20
mvaNick Polyak6-Mar-08 15:20 

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.