/// <summary> /// Processes a drag in the main grid. /// </summary> private void OnMainGridMouseMove(object sender, MouseEventArgs e) { // Exit if shift key and left mouse button aren't pressed if (e.LeftButton != MouseButtonState.Pressed) { return; } if (Keyboard.Modifiers != ModifierKeys.Shift) { return; } /* We use the m_MouseDirection value in the * OnMainGridCheckDropTarget() event handler. */ // Find the row the mouse button was pressed on var row = FindVisualParent <DataGridRow>(e.OriginalSource as FrameworkElement); m_OriginalIndex = row.GetIndex(); // If the row was already selected, begin drag if ((row != null) && row.IsSelected) { // Get the grocery item represented by the selected row var selectedItem = (GroceryItem)row.Item; var finalDropEffect = DragDrop.DoDragDrop(row, selectedItem, DragDropEffects.Move); if ((finalDropEffect == DragDropEffects.Move) && (m_TargetItem != null)) { /* A drop was accepted. Determine the index of the item being * dragged and the drop location. If they are different, then * move the selectedItem to the new location. */ // Move the dragged item to its drop position var oldIndex = m_ViewModel.GroceryList.IndexOf(selectedItem); var newIndex = m_ViewModel.GroceryList.IndexOf(m_TargetItem); if (oldIndex != newIndex) { m_ViewModel.GroceryList.Move(oldIndex, newIndex); } m_TargetItem = null; } } }
/// <summary> /// Process a row drop on the DataGrid. /// </summary> private void OnMainGridDrop(object sender, DragEventArgs e) { e.Effects = DragDropEffects.None; e.Handled = true; // Verify that this is a valid drop and then store the drop target var row = FindVisualParent <DataGridRow>(e.OriginalSource as UIElement); if (row != null) { m_TargetItem = row.Item as GroceryItem; if (m_TargetItem != null) { e.Effects = DragDropEffects.Move; } } // Erase last drop-line if (m_OldRow != null) { m_OldRow.BorderThickness = new Thickness(0, 0, 0, 0); } }