Click here to Skip to main content
6,595,444 members and growing! (21,476 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » Windows Presentation Foundation » General     Intermediate License: The Code Project Open License (CPOL)

MultiSelect Drag and Drop in WPF

By Nick Polyak

Discusses implementation of Drag and Drop functionality for multiselected items in ListBox/ListView
C# (C# 3.0), Windows, .NET (.NET 3.0, .NET 3.5), XAML, WPF, Dev
Posted:5 Mar 2008
Updated:6 Mar 2008
Views:17,746
Bookmarked:16 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
8 votes for this article.
Popularity: 2.23 Rating: 2.47 out of 5
3 votes, 37.5%
1
2 votes, 25.0%
2

3

4
3 votes, 37.5%
5
MultiSelectImage2.JPG

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:

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:

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

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

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):

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

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

// 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:

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)

About the Author

Nick Polyak


Member
I have 15 years of experience developing enterprise software, starting from C++ and Java on UNIX and moving towards C# on Windows platforms.
I am facinated by the new .NET technologies especially WPF, Silverlight 2.0 and LINQ.
Recently I decided to make a move and start my own contracting consulting and mentoring company AWebPros.
I can be contacted via my web site awebpros.com or through my blog at nickssoftwareblog.com
Occupation: Architect
Company: AWebPros
Location: United States United States

Other popular Windows Presentation Foundation articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 7 of 7 (Total in Forum: 7) (Refresh)FirstPrevNext
GeneralMy vote of 1 PinmemberMa3ztro5:17 7 May '09  
GeneralMy vote of 1 Pinmemberzbend12:42 30 Jan '09  
GeneralBug? Pinmembercherishnews15:05 18 Jan '09  
GeneralRe: Bug? PinmemberNick Polyak15:24 18 Jan '09  
GeneralRe: Bug? PinmemberNick Polyak15:27 18 Jan '09  
QuestionWhere's screens? Pinmembersotona0:36 6 Mar '08  
GeneralRe: Where's screens? PinmemberNick Polyak16:20 6 Mar '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 6 Mar 2008
Editor: Deeksha Shenoy
Copyright 2008 by Nick Polyak
Everything else Copyright © CodeProject, 1999-2009
Web16 | Advertise on the Code Project