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

时间:2013-12-18 20:09:27

标签: c# wpf drag-and-drop

所以我在WPF中有点拖拉。使用shift键可以正常工作。

但是,如果我使用ctrl键开始拖动操作,则dodraganddrop方法运行正常。但是当我按住ctrl键时,光标变为Drop Not Allowed Symbol,我不能放下项目,直到我放开控制键。为什么这样,我该如何改变呢?我认为通过将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

}

1 个答案:

答案 0 :(得分:3)

在拖放操作期间按住键盘上的Ctrl与复制项目有关,因此您还需要允许DragDropEffects.Copy枚举。您可以通过DoDragDrop方法设置此操作来允许这两种操作:

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

你也可以使用它:

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