Click here to Skip to main content
15,881,413 members
Articles / Desktop Programming / WPF
Tip/Trick

Reorder DataGrid/ListBox Items with Drag and Drop Wpf

Rate me:
Please Sign up or sign in to vote.
4.63/5 (7 votes)
7 Aug 2014CPOL 32.6K   2.9K   5   2
The easy way to Reorder your DataGrid/ListBox with Drag and Drop

Introduction

Drag and Drop usually ameliorate our project spacially on terme of use and simplicity. 

Using the code

First of all , you need to add to the reference the dll GongSolutions.Wpf.DragDrop , it is already unclude on the Project after you downoad it.

Then we add the path of this dll in our MainPage.

C++
 xmlns:dd="clr-namespace:GongSolutions.Wpf.DragDrop;assembly=GongSolutions.Wpf.DragDrop"

After we create our ListBox or DataGrid , we add  attributes dd:DragDrop.IsDragSource and  dd:DragDrop.IsDropTarget="True".

<ListBox Grid.Column="1" SelectionMode="Extended" ItemsSource="{Binding MSPCollection}"
 dd:DragDrop.IsDragSource="True" Width="300" Margin="0,0,5,0" dd:DragDrop.IsDropTarget="True">
...

After We Bind a List of Object to this Listbox we add two method DragOver and Drop.

Ps: You class should inherit the interface IDropTarget.

void IDropTarget.DragOver(DropInfo dropInfo)
       {

       }

void IDropTarget.Drop(DropInfo dropInfo)
       {

       }

History

Download this simple example to get the full project ;)

 

Image 1

License

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


Written By
Software Developer (Junior) Microsoft Student Partners
Tunisia Tunisia
I study Software Engineering , 23 years old , I'm motivated with all Technologies of Microsoft.
Since I have been in the Community of Microsoft as Microsoft Student Partners, I developped many apps on the platform Windows and Phone. Now , it's time to share what I learn here and I'am ready to help Everyone.
You can contact me at any time (anisderbel@outlook.com)
This is a Organisation

9 members

Comments and Discussions

 
GeneralMy vote of 5 Pin
Unfug11-May-17 22:40
Unfug11-May-17 22:40 
QuestionInvalid Markup Pin
Bermudabob688-Dec-14 23:56
Bermudabob688-Dec-14 23: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.