Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / desktop / WPF

Reorder DataGrid/ListBox Items with Drag and Drop Wpf

4.63/5 (7 votes)
7 Aug 2014CPOL 33.6K   2.9K  
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)