James Joshua Street
James Joshua Street

Reputation: 3419

WPF how to get drag and drop to work with control key?

So I have drag and drop somewhat working in WPF. it works fine with the shift key.

However, if I use the ctrl key to start a drag operation the dodraganddrop method runs fine. But while I hold the ctrl key the cursor changes to a Drop Not Allowed Symbol and the I cannot drop the items until I let go of the control key. Why is this and how can I change it? I thought the cursor was determined by setting DragEventArgs.Effects to a DragDropEffect, but I have set it to DragDropEffectsMove and I still get the no drop allowed cursor.

    private void NameListView_MouseMove(object sender, MouseEventArgs e)
    {

      // Get the current mouse position
      Point mousePos = e.GetPosition(null);
      Vector diff = NameListViewMouseDownStartPoint - mousePos;

      if (e.LeftButton == MouseButtonState.Pressed &&
          (Math.Abs(diff.X) > SystemParameters.MinimumHorizontalDragDistance ||
          Math.Abs(diff.Y) > SystemParameters.MinimumVerticalDragDistance)
        )
      {
        if (Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift) || Keyboard.IsKeyDown(Key.LeftCtrl)
            || Keyboard.IsKeyDown(Key.RightCtrl))
        {
          DataObject do1 = new DataObject("AddedItemsFormat", _GraphViewerViewModel.SelectedAddedItems);
          DragDrop.DoDragDrop(NameListView, do1, DragDropEffects.Move);
        }
      }
    }

private void NameListView_DragOver(object sender, DragEventArgs e)
{
  e.Effects = DragDropEffects.Move;

}

// Moves the item to the location of the insertion mark. 
private void NameListView_DragDrop(object sender, DragEventArgs e)
{
  //Drop Code. This code will not run unless the user first lets go of the control key so he can drop here

}

Upvotes: 2

Views: 2750

Answers (1)

Sheridan
Sheridan

Reputation: 69985

Holding the Ctrl key on the keyboard during a drag and drop operation relates to copying the item and therefore you need to allow the DragDropEffects.Copy enumeration also. You can allow both operations by setting this in the DoDragDrop method:

DragDrop.DoDragDrop(NameListView, do1, DragDropEffects.Copy | DragDropEffects.Move);

You could also use this:

DragDrop.DoDragDrop(NameListView, do1, DragDropEffects.All);

Upvotes: 4

Related Questions