diff --git a/README.md b/README.md index c3fc145..cf0f7cb 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,352 @@ # How to drag and drop rows between datagrid and listview in wpf? This example illustrates how to drag and drop rows between datagrid and listview in wpf + +## Sample +## UWP + +```xaml + + + + + + + + + + + + + + + + + + + +C#: + +this.datagrid.RowDragDropController = new GridRowDragDropControllerExt(); +this.listView.DragItemsStarting += ListView_DragItemsStarting; +this.listView.DragOver += ListView_DragOver; +this.listView.Drop += ListView_Drop; +this.listView.DragEnter += ListView_DragEnter; + +private void ListView_DragEnter(object sender, DragEventArgs e) +{ + e.AcceptedOperation = Windows.ApplicationModel.DataTransfer.DataPackageOperation.Copy; +} + +private void ListView_Drop(object sender, DragEventArgs e) +{ + foreach (var item in records1) + { + this.datagrid.View.Remove(item as BusinessObjects); + + (this.DataContext as ViewModel).GDCSource1.Add(item as BusinessObjects); + } +} + +ObservableCollection records1 = new ObservableCollection(); + +private void ListView_DragOver(object sender, DragEventArgs e) +{ + if (e.DataView.Properties.ContainsKey("Records")) + records1 = e.DataView.Properties["Records"] as ObservableCollection; +} + +private void ListView_DragItemsStarting(object sender, DragItemsStartingEventArgs e) +{ + var records = new ObservableCollection(); + records.Add(listView.SelectedItem); + e.Data.Properties.Add("DraggedItem", records); + e.Data.Properties.Add("ListView", listView); + e.Data.SetText(StandardDataFormats.Text); +} + +public class GridRowDragDropControllerExt : GridRowDragDropController +{ + ObservableCollection draggingRecords = new ObservableCollection(); + + protected override void ProcessOnDragOver(DragEventArgs args, RowColumnIndex rowColumnIndex) + { + if (args.DataView.Properties.ContainsKey("DraggedItem")) + draggingRecords = args.DataView.Properties["DraggedItem"] as ObservableCollection; + + else + draggingRecords = args.DataView.Properties["Records"] as ObservableCollection; + + if (draggingRecords == null) + return; + + var dropPosition = GetDropPosition(args, rowColumnIndex, draggingRecords); + + if (dropPosition == DropPosition.None) + { + CloseDragIndicators(); + args.AcceptedOperation = DataPackageOperation.None; + args.DragUIOverride.Caption = "Can't drop here"; + return; + } + + else if (dropPosition == DropPosition.DropAbove) + { + if (draggingRecords != null && draggingRecords.Count > 1) + args.DragUIOverride.Caption = "Drop these " + draggingRecords.Count + " rows above"; + else + { + args.AcceptedOperation = DataPackageOperation.Copy; + args.DragUIOverride.IsCaptionVisible = true; + args.DragUIOverride.IsContentVisible = true; + args.DragUIOverride.IsGlyphVisible = true; + args.DragUIOverride.Caption = "Drop above"; + } + } + else + { + if (draggingRecords != null && draggingRecords.Count > 1) + args.DragUIOverride.Caption = "Drop these " + draggingRecords.Count + " rows below"; + else + args.DragUIOverride.Caption = "Drop below"; + } + args.AcceptedOperation = DataPackageOperation.Move; + ShowDragIndicators(dropPosition, rowColumnIndex, args); + args.Handled = true; + } + + ListView listview; + + protected override void ProcessOnDrop(DragEventArgs args, RowColumnIndex rowColumnIndex) + { + listview = null; + + if (args.DataView.Properties.ContainsKey("ListView")) + listview=args.DataView.Properties["ListView"] as ListView; + + if (!DataGrid.SelectionController.CurrentCellManager.CheckValidationAndEndEdit()) + return; + + var dropPosition = GetDropPosition(args, rowColumnIndex, draggingRecords); + if (dropPosition == DropPosition.None) + return; + + var droppingRecordIndex = this.DataGrid.ResolveToRecordIndex(rowColumnIndex.RowIndex); + + if (droppingRecordIndex < 0) + return; + + foreach (var record in draggingRecords) + { + if (listview != null) + { + (listview.ItemsSource as ObservableCollection).Remove(record as BusinessObjects); + var sourceCollection = this.DataGrid.View.SourceCollection as IList; + + if (dropPosition == DropPosition.DropBelow) + sourceCollection.Insert(droppingRecordIndex + 1, record); + else + sourceCollection.Insert(droppingRecordIndex, record); + } + else + { + var draggingIndex = this.DataGrid.ResolveToRowIndex(draggingRecords[0]); + + if (draggingIndex < 0) + { + return; + } + + var recordindex = this.DataGrid.ResolveToRecordIndex(draggingIndex); + var recordEntry = this.DataGrid.View.Records[recordindex]; + this.DataGrid.View.Records.Remove(recordEntry); + 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)); + } + } + CloseDragIndicators(); + } +} +``` + +## WPF + +```xaml + + + + + + + + + . . . + . . . + + + + + + + + + + + + + + + + + + + + + + + + + + + + +C#: + +AssociatedObject.sfDataGrid.RowDragDropController.Drop += RowDragDropController_Drop; +AssociatedObject.listView.PreviewMouseMove += ListView_PreviewMouseMove; +AssociatedObject.listView.Drop += ListView_Drop; + +private void ListView_Drop(object sender, DragEventArgs e) +{ + ObservableCollection DraggingRecords = new ObservableCollection(); + if (e.Data.GetDataPresent("ListViewRecords")) + { + DraggingRecords = e.Data.GetData("ListViewRecords") as ObservableCollection; + var listViewRecord = DraggingRecords[0] as Orders; + (AssociatedObject.listView.ItemsSource as ObservableCollection).Remove(listViewRecord); + (this.AssociatedObject.DataContext as ViewModel).OrderDetails1.Add(listViewRecord); + } + else + { + DraggingRecords = e.Data.GetData("Records") as ObservableCollection; + var record = DraggingRecords[0] as Orders; + this.AssociatedObject.sfDataGrid.View.Remove(record); + (this.AssociatedObject.DataContext as ViewModel).OrderDetails1.Add(record); + } +} + +private void ListView_PreviewMouseMove(object sender, System.Windows.Input.MouseEventArgs e) +{ + if (e.LeftButton == MouseButtonState.Pressed) + { + ListBox dragSource = null; + var records = new ObservableCollection(); + ListBox parent = (ListBox)sender; + dragSource = parent; + object data = GetDataFromListBox(dragSource, e.GetPosition(parent)); + records.Add(data); + + var dataObject = new DataObject(); + dataObject.SetData("ListViewRecords", records); + dataObject.SetData("ListView", AssociatedObject.listView); + + if (data != null) + { + DragDrop.DoDragDrop(parent, dataObject, DragDropEffects.Move); + } + + } + e.Handled = true; +} + +private static object GetDataFromListBox(ListBox source, Point point) +{ + UIElement element = source.InputHitTest(point) as UIElement; + if (element != null) + { + object data = DependencyProperty.UnsetValue; + while (data == DependencyProperty.UnsetValue) + { + data = source.ItemContainerGenerator.ItemFromContainer(element); + if (data == DependencyProperty.UnsetValue) + { + element = VisualTreeHelper.GetParent(element) as UIElement; + } + if (element == source) + { + return null; + } + } + if (data != DependencyProperty.UnsetValue) + { + return data; + } + } + return null; +} + +private void RowDragDropController_Drop(object sender, Syncfusion.UI.Xaml.Grid.GridRowDropEventArgs e) +{ + if (e.IsFromOutSideSource) + { + ObservableCollection DraggingRecords = new ObservableCollection(); + if (e.Data.GetDataPresent("ListViewRecords")) + DraggingRecords = e.Data.GetData("ListViewRecords") as ObservableCollection; + else + DraggingRecords = e.Data.GetData("Records") as ObservableCollection; + + var draggingRecords = DraggingRecords[0] as Orders; + int dropIndex = (int)e.TargetRecord; + var dropPosition = e.DropPosition.ToString(); + IList collection = AssociatedObject.sfDataGrid.View.SourceCollection as IList; + + if (dropPosition == "DropAbove") + { + dropIndex--; + collection.Insert(dropIndex, draggingRecords); + } + else + { + dropIndex++; + collection.Insert(dropIndex, draggingRecords); + } + + (AssociatedObject.listView.ItemsSource as ObservableCollection).Remove(draggingRecords as Orders); + e.Handled = true; + } +} +``` \ No newline at end of file