简体   繁体   English

WPF如何使用控制键进行拖放操作?

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

So I have drag and drop somewhat working in WPF. 所以我在WPF中有点拖拉。 it works fine with the shift key. 使用shift键可以正常工作。

However, if I use the ctrl key to start a drag operation the dodraganddrop method runs fine. 但是,如果我使用ctrl键开始拖动操作,dodraganddrop方法运行正常。 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. 但是当我按住ctrl键时,光标变为Drop Not Allowed Symbol,我不能放下项目,直到我放开控制键。 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. 我认为通过将DragEventArgs.Effects设置为DragDropEffect来确定光标,但我已将其设置为DragDropEffectsMove并且仍然获得了无删除光标。

    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

}

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. 在拖放操作期间Ctrl键盘上的Ctrl键与复制项目有关,因此您还需要允许DragDropEffects.Copy枚举。 You can allow both operations by setting this in the DoDragDrop method: 您可以通过在DoDragDrop方法中设置此操作来允许这两个操作:

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

You could also use this: 你也可以用这个:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM