简体   繁体   English

如何从Winforms应用程序中的小型WPF元素主机实现拖放?

[英]How do you implement drag and drop from a small WPF Element host in a Winforms application?

In our app we host a small WPF Listbox inside of an Element host, we implement drag and drop using the PreviewMouseDown event... 在我们的应用程序中,我们在Element主机内部托管一个小的WPF Listbox,我们使用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);                
            } 

        }

The problem we are seeing is that when I click and drag an item fairly quickly, the WPF control only fires one PreviewMouseMove event before the mouse leaves the Elementhost, therefore the drag operation is not started until the mouse is returned to the Elementhost and another PreviewMouseMove event is raised. 我们看到的问题是,当我快速单击并拖动项目时,WPF控件仅在鼠标离开Elementhost之前触发一个PreviewMouseMove事件,因此在鼠标返回到Elementhost和另一个PreviewMouseMove之前不会启动拖动操作事件被提出。

Is there a robust way of handling this case? 有没有一种强有力的方法来处理这种情况?

You have to capture the mouse on the mouse down event. 您必须在鼠标按下事件上捕获鼠标。 Any mouse moves after that are always routed to your window, even if the cursor is no longer hovering it. 任何鼠标移动之后总是路由到您的窗口,即使光标不再悬停它。 Use the Mouse.Capture() method in WPF. 在WPF中使用Mouse.Capture()方法。

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

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