【问题标题】:Spurious drop events after drag drop operation completes拖放操作完成后的虚假拖放事件
【发布时间】:2021-08-09 09:51:36
【问题描述】:

我已经在我的 WPF 应用程序中实现了拖动到外部应用程序,并且它可以工作。然而,在我将光标放回目标后,我继续在 my 应用程序上收到虚假的拖放事件。这会“永远”持续下去。

据我在调用 DragDrop.DoDragDrop() 后从在线搜索中得知,我不需要做任何其他事情。我尝试添加一些代码来取消事件,但即使它运行也没有效果:

        private void UIElement_OnPreviewMouseMove(object sender, MouseEventArgs e)
    {
        var frameworkElement = (FrameworkElement) sender;
        if (frameworkElement.DataContext is IDragSource dragSource)
        {
            var position = e.GetPosition((IInputElement) sender);
            if (dragSource.DragCanStart(new Point {X = position.X, Y = position.Y}))
            {
                var dragStart = true;

                void OnFrameworkElementOnQueryContinueDrag(object o, QueryContinueDragEventArgs e2)
                {
                    if (!dragStart)
                    {
                        e2.Action = DragAction.Cancel;
                        frameworkElement.QueryContinueDrag -= OnFrameworkElementOnQueryContinueDrag;
                    }
                }

                frameworkElement.QueryContinueDrag += OnFrameworkElementOnQueryContinueDrag;
                dragSource.DragInitiate(sender);
                e.Handled = true;
                dragStart = false;
            }
        }
    }

【问题讨论】:

    标签: wpf drag-and-drop


    【解决方案1】:

    我已经追踪到我们的代码启动了多次拖动。我们的保护逻辑不够好,这意味着每次鼠标移到源组件上时都会触发另一次拖动。

    很难调试:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-21
      • 2016-09-11
      • 2012-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多