public class GridRowDragDropControllerExt : GridRowDragDropController
{
ObservableCollection<object> draggingRecords = new ObservableCollection<object>();
/// <summary>
/// Occurs when the input system reports an underlying dragover event with this element as the potential drop target.
/// </summary>
/// <param name="args">An <see cref="T:Windows.UI.Xaml.DragEventArgs">DragEventArgs</see> that contains the event data.</param>
/// <param name="rowColumnIndex">Specifies the row column index based on the mouse point.</param>
protected override void ProcessOnDragOver(DragEventArgs args, RowColumnIndex rowColumnIndex)
{
if (args.Data.GetDataPresent("ListViewRecords"))
draggingRecords = args.Data.GetData("ListViewRecords") as ObservableCollection<object>;
else
draggingRecords = args.Data.GetData("Records") as ObservableCollection<object>;
if (draggingRecords == null)
return;
//To highlight the target record while dragging
var targetRecordIndex = this.DataGrid.ResolveToRecordIndex(rowColumnIndex.RowIndex);
this.DataGrid.SelectedIndex = targetRecordIndex;
//To get the dropping position of the record
var dropPosition = GetDropPosition(args, rowColumnIndex, draggingRecords);
//To Show the draggable popup with the DropAbove/DropBelow message
ShowDragDropPopup(dropPosition, draggingRecords, args);
//To Show the up and down indicators while dragging the row
ShowDragIndicators(dropPosition, rowColumnIndex, args);
args.Handled = true;
}
ListView listview;
/// <summary>
/// Occurs when the input system reports an underlying drop event with this element as the drop target.
/// </summary>
/// <param name="args">An <see cref="T:Windows.UI.Xaml.DragEventArgs">DragEventArgs</see> that contains the event data.</param>
/// <param name="rowColumnIndex">Specifies the row column index based on the mouse point.</param>
protected override void ProcessOnDrop(DragEventArgs args, RowColumnIndex rowColumnIndex)
{
if (args.Data.GetDataPresent("ListView"))
listview = args.Data.GetData("ListView") as ListView;
if (!DataGrid.SelectionController.CurrentCellManager.CheckValidationAndEndEdit())
return;
//To get the dropping position of the record
var dropPosition = GetDropPosition(args, rowColumnIndex, draggingRecords);
if (dropPosition == DropPosition.None)
return;
// to get the index of dropping record
var droppingRecordIndex = this.DataGrid.ResolveToRecordIndex(rowColumnIndex.RowIndex);
if (droppingRecordIndex < 0)
return;
// to insert the dragged records based on dropping records index
foreach (var record in draggingRecords)
{
if (listview != null)
{
(listview.ItemsSource as ObservableCollection<Orders>).Remove(record as Orders);
var sourceCollection = this.DataGrid.View.SourceCollection as IList;
if (dropPosition == DropPosition.DropBelow)
{
sourceCollection.Insert(droppingRecordIndex + 1, record);
//To highlight the dropped record
this.DataGrid.SelectedIndex = droppingRecordIndex + 1;
}
else
{
sourceCollection.Insert(droppingRecordIndex, record);
//To highlight the dropped record
this.DataGrid.SelectedIndex = droppingRecordIndex;
}
}
else
{
var draggingIndex = this.DataGrid.ResolveToRowIndex(draggingRecords[0]);
if (draggingIndex < 0)
{
return;
}
// to get the index of dragging row
var recordindex = this.DataGrid.ResolveToRecordIndex(draggingIndex);
// to ger the record based on index
var recordEntry = this.DataGrid.View.Records[recordindex];
this.DataGrid.View.Records.Remove(recordEntry);
var droppingIndex = droppingRecordIndex;
// to insert the dragged records to particular position
if (draggingIndex < rowColumnIndex.RowIndex && dropPosition == DropPosition.DropAbove)
this.DataGrid.View.Records.Insert(droppingRecordIndex - 1, this.DataGrid.View.Records.CreateRecord(record));
else if (draggingIndex > rowColumnIndex.RowIndex && dropPosition == DropPosition.DropBelow)
this.DataGrid.View.Records.Insert(droppingRecordIndex + 1, this.DataGrid.View.Records.CreateRecord(record));
else
this.DataGrid.View.Records.Insert(droppingRecordIndex, this.DataGrid.View.Records.CreateRecord(record));
}
}
//Closes the Drag arrow indication all the rows
CloseDragIndicators();
//Closes the Drag arrow indication all the rows
CloseDraggablePopUp();
}
} |