65.9K
CodeProject is changing. Read more.
Home

Reorder DataGrid/ListBox Items with Drag and Drop Wpf

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.63/5 (7 votes)

Aug 7, 2014

CPOL
viewsIcon

33567

downloadIcon

2874

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.

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