The way to make DataGrid support change row order by Drag & Drop

Nov 27, 2008 at 8:38 AM
Edited Nov 27, 2008 at 8:42 AM
John Smith Provide a sample of Drag and Drop Items in a WPF ListView on CodeProject.
Following is the link.
http://www.codeproject.com/KB/WPF/ListViewDragDropManager.aspx

The helper class to manage Drag and Drop ListViewItem in ListView can be easily ported to implement the function of Drag and Drop DataGridRow in DataGrid.

Only a few modification is needed, such as change ListView to DataGrid, change ListViewItem to DataGridRow. I tried to modification it use generic to support both ListView and DataGrid, but fails. So at last I modification it to a DataGrid version.

There may be one defect existed in ListViewDragDropManager.cs, it may fail to work if the Virtualization is turned on. VirtualizingStackPanel.VirtualizationMode="Recycling"
VirtualizingStackPanel.IsVirtualizing="true"

At least the function IndexUnderDragCursor() does not work on DataGrid if the Virtualization is turned on. Because of RowVirtualization , The ItemContainer of invisible rows is recycled, the item is null under such situation. So we need check whether the ItemContainer got is null or not before use it.After such modification, it will work well when Virtualization is turned on.