c# - 如何在 Winforms 应用程序中实现从小型 WPF 元素宿主的拖放操作?

标签 c# wpf drag-and-drop winforms-interop

在我们的应用程序中,我们在元素宿主中托管了一个小型 WPF 列表框,我们使用 PreviewMouseDown 事件实现拖放...

   private void Border_PreviewMouseMove(object sender, MouseEventArgs e)
        {
            // Get the current mouse position
            Point mousePos = e.GetPosition(null);
            Vector diff = startPoint - mousePos;

            if (e.LeftButton == MouseButtonState.Pressed &&
                Math.Abs(diff.X) > SystemParameters.MinimumHorizontalDragDistance &&
                Math.Abs(diff.Y) > SystemParameters.MinimumVerticalDragDistance)
            {
                OnDragStarted(e);                
            } 

        }

我们看到的问题是,当我相当快地单击并拖动一个项目时,WPF 控件仅在鼠标离开 Elementhost 之前触发一个 PreviewMouseMove 事件,因此直到鼠标返回到 Elementhost 才开始拖动操作并引发另一个 PreviewMouseMove 事件。

是否有可靠的方法来处理这种情况?

最佳答案

您必须在鼠标按下事件上捕获鼠标。此后的任何鼠标移动都会始终路由到您的窗口,即使光标不再悬停在窗口上也是如此。在 WPF 中使用 Mouse.Capture() 方法。

关于c# - 如何在 Winforms 应用程序中实现从小型 WPF 元素宿主的拖放操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5304869/

相关文章:

c# - ASP.NET 使用 SqlConnection 连接 MySQL

c# - 如何将 ItemsSource 绑定(bind)到多个集合?

.net - PropertyChangedEventManager 何时创建以及何时附加?

c# - C# 中的 c++ "_popen"是什么?

c# - 使用 Kendo Grid,如何更改工具栏中 "Create"按钮上的文字?

C# - 确定参数是否已传递失败的方法

c# - WPF DataGridComboBoxColumn 绑定(bind)?

javascript - 如何复制拖放事件?

javascript - 如何使 Canvas 的不同形状可拖动且其特定区域可放置在同一 Canvas 中

javascript - 拖放 : How to get the URL of image being dropped if image is a link (not the url of the link)