【问题标题】:WPF how to get drag and drop to work with control key?WPF如何让拖放与控制键一起工作?
【发布时间】:2013-12-19 09:35:12
【问题描述】:

所以我在 WPF 中进行了一些拖放操作。使用 shift 键效果很好。

但是,如果我使用 ctrl 键开始拖动操作,dodraganddrop 方法运行良好。但是,当我按住 ctrl 键时,光标变为“不允许放置符号”,并且在放开控制键之前,我无法放置项目。为什么会这样,我该如何改变它?我认为光标是通过将 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

}

【问题讨论】:

    标签: c# wpf drag-and-drop


    【解决方案1】:

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

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

    你也可以这样用:

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

    【讨论】:

    • 所以我尝试了这个并没有任何效果,但后来我将拖放修改为 DragDrop.DoDragDrop(AvailableSignalsListView, do1, DragDropEffects.All);它解决了我的问题。如果你能修改你的答案,我会给你信用,因为你解决了我的问题。我认为 e.Effects 控制的鼠标光标类型是正确的,但实际允许或不允许由 dodraganddrop 参数确定
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-02
    • 1970-01-01
    相关资源
    最近更新 更多