/// <summary> /// Handles the MouseMove event of the DataGrid control. Implements row rearranging functionality via drag-and-drop. Handles the /// drag event. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e"> /// The <see cref="MouseEventArgs" /> instance containing the event data. /// </param> private void dataGrid_MouseMove(object sender, MouseEventArgs e) { BindingList <LivePlayerBoxScore> pbsList = Equals(sender, dgvPlayersAway) ? _lpbsAwayList : _lpbsHomeList; // This is what we're using as a cue to start a drag, but this can be // customized as needed for an application. if (e.LeftButton == MouseButtonState.Pressed) { // Find the row and only drag it if it is already selected. var row = findVisualParent <DataGridRow>(e.OriginalSource as FrameworkElement); if ((row != null) && row.IsSelected) { // Perform the drag operation var selectedPerson = (LivePlayerBoxScore)row.Item; var finalDropEffect = DragDrop.DoDragDrop(row, selectedPerson, DragDropEffects.Move); if ((finalDropEffect == DragDropEffects.Move) && (_targetPerson != null)) { // A Move drop was accepted // Determine the index of the item being dragged and the drop // location. If they are difference, then move the selected // item to the new location. var oldIndex = pbsList.IndexOf(selectedPerson); var newIndex = pbsList.IndexOf(_targetPerson); if (oldIndex != newIndex) { pbsList.Insert(newIndex + 1, selectedPerson); pbsList.RemoveAt(oldIndex); } _targetPerson = null; } } } }
/// <summary> /// Handles the Drop event of the DataGrid control. Implements row rearranging functionality via drag-and-drop. Handles the drop /// event. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e"> /// The <see cref="DragEventArgs" /> instance containing the event data. /// </param> private void dataGrid_Drop(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) { _targetPerson = row.Item as LivePlayerBoxScore; if (_targetPerson != null) { e.Effects = DragDropEffects.Move; } } }