diff --git a/README.md b/README.md index c3fc145..4cf4bff 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,143 @@ -# 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 +# How to drag and drop rows between datagrid and listview in wpf + +This example illustrates how to drag and drop rows between WPF and UWP DataGrids and listview. + +To perform dragging between the `ListView` and `SfDataGrid`, by using the `GridRowDragDropController.DragStart` and `GridRowDragDropController.Drop` events. And you must set the `AllowDrop` property as `true` in the `ListView` while doing the drag and drop operation from `SfDataGrid` with `ListView` control. + +``` c# +this.dataGrid.RowDragDropController.DragStart += sfGrid_DragStart; +this.dataGrid.RowDragDropController.Drop += sfGrid_Drop; +this.listView.PreviewMouseMove += ListView_PreviewMouseMove; +this.listView.Drop += ListView_Drop; + +/// +/// customize the DragStart event.Restrict the certain record from dragging. +/// +/// +/// +private void sfGrid_DragStart(object sender, GridRowDragStartEventArgs e) +{ + var draggingRecords = e.DraggingRecords[0] as OrderInfo; + if (draggingRecords.CustomerName == "Martin") + { + e.Handled = true; + } +} + +/// +/// Customize the Drop event.restrict the certain record and Drop position from drop. +/// +/// +/// +private void sfGrid_Drop(object sender, 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 OrderInfo; + int dropIndex = (int)e.TargetRecord; + var dropPosition = e.DropPosition.ToString(); + IList collection = AssociatedObject.sfGrid.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 OrderInfo); + e.Handled = true; + + } +} + +/// +/// List view initiates the DragDrop operation +/// +/// +/// +private void ListView_PreviewMouseMove(object sender, 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; +} + +/// +/// Get the data from list box control +/// +/// +/// +/// +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; +} + +/// +/// ListView Drop event. +/// +/// +/// +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 OrderInfo; + (AssociatedObject.listView.ItemsSource as ObservableCollection).Remove(listViewRecord); + (this.AssociatedObject.DataContext as ViewModel).OrdersListView.Add(listViewRecord ); + } + else + { + DraggingRecords = e.Data.GetData("Records") as ObservableCollection; + var record = DraggingRecords[0] as OrderInfo; + this.AssociatedObject.sfGrid.View.Remove(record); + (this.AssociatedObject.DataContext as ViewModel).OrdersListView.Add(record); + } +} +``` \ No newline at end of file