Wpf 拇指拖动不会在其他组件上触发拖动输入

发布于 2024-12-09 14:40:11 字数 169 浏览 0 评论 0原文

我有一个带有一些 Thumb 控件的 wpf 应用程序,这些控件可以在画布上移动项目。我想检测这些拇指何时被拖动到另一个元素上。但是,不会触发被拖过的元素上的拖动输入。我知道这段代码的作用是拖动项目而不是拇指触发事件。

拇指上的拖动事件不是其他组件监听的拖动事件吗?

知道如何让它发挥作用吗?

I have a wpf application with some Thumb controls which move items around a canvs. I want to detect when these thumbs are dragged over another element. However, the drag enter on the element which is dragged over is not fired. I know this code works as drag items that are not thumbs fire the event.

Is the drag event on the thumb not a drag event that other components listen to?

Any idea how to get this to work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

神妖 2024-12-16 14:40:11

显然没有。唯一与 Thumb 相关的事件是 DragStarted、DragCompleted 和 DragDelta。其他事件用于拖放,例如 DragEnter 事件。这些事件特别适用于拖放操作,例如将文件从资源管理器拖到应用程序中,与 Thumb 无关。名称相似但实际上却截然不同。

您可以尝试的一件事是在拖动时使用 HitTesting,但请记住,拖放和拖动拇指会捕获鼠标,这会禁用其他类上的输入事件。

Apparently no. The only Thumb related Events are DragStarted, DragCompleted and DragDelta. The other events are for Drag and drop, like your DragEnter Event. These Events are especially for the Drag and drop, like dragging a file from the explorer into your application which has nothing to do with the Thumb. The names are similar but in fact very different.

One thing you could try is to use HitTesting while dragging, but remember that drag and drop and dragging a thumb takes the mouse capture, which disables input events on the other classes.

债姬 2024-12-16 14:40:11

为了在拇指对象移动到另一个组件上时模拟拖动输入事件,我必须执行以下操作:

为拇指拖动增量注册事件处理程序

EventManager.RegisterClassHandler(typeof(Thumb), Thumb.DragDeltaEvent, new RoutedEventHandler(Thumb_DragDeltaEvent), true);

然后在事件处理程序中查看拖动的元素是否位于所拖动的元素上方监听移动组件

void Thumb_DragDeltaEvent(object sender, RoutedEventArgs e)
    {
        UIElement src = e.Source as UIElement ;
        if (src != null)
        {                
            Point srcPositionTopLeft = new Point(Canvas.GetLeft(src), Canvas.GetTop(src));
            Point srcPositionBottomRight = new Point(srcPositionTopLeft.X + src.ActualWidth, srcPositionTopLeft.Y + ActualHeight);
            Rect srcRect = new Rect(srcPositionTopLeft, srcPositionBottomRight);
            Rect transformedSrcRect = src.TransformToAncestor(this.Parent).TransformBounds(srcRect);

            Point trgPositionTopLeft = new Point(Canvas.GetLeft(this), Canvas.GetTop(this));
            Point trgPositionBottomRight = new Point(trgPositionTopLeft.X + this.ActualWidth, trgPositionTopLeft.Y + this.ActualHeight);
            Rect trgRect = new Rect(srcPositionTopLeft, srcPositionBottomRight);
            Rect transformedTrgRect = this.TransformToAncestor(this.Parent).TransformBounds(trgRect);

            if (transformedSrcRect.Contains(transformedTrgRect) ||
                transformedSrcRect.IntersectsWith(transformedTrgRect))
            {
                //drag is over element
            }
        }
    }

记住稍后删除事件处理程序等。

希望这对将来的人有帮助。

To achieve the simulation of a drag enter event when a thumb object is moved over another component I've had to do this:

Register event handler for the thumb drag delta

EventManager.RegisterClassHandler(typeof(Thumb), Thumb.DragDeltaEvent, new RoutedEventHandler(Thumb_DragDeltaEvent), true);

Then in the event handler see if the dragged element is over the element that is listening to the moving component

void Thumb_DragDeltaEvent(object sender, RoutedEventArgs e)
    {
        UIElement src = e.Source as UIElement ;
        if (src != null)
        {                
            Point srcPositionTopLeft = new Point(Canvas.GetLeft(src), Canvas.GetTop(src));
            Point srcPositionBottomRight = new Point(srcPositionTopLeft.X + src.ActualWidth, srcPositionTopLeft.Y + ActualHeight);
            Rect srcRect = new Rect(srcPositionTopLeft, srcPositionBottomRight);
            Rect transformedSrcRect = src.TransformToAncestor(this.Parent).TransformBounds(srcRect);

            Point trgPositionTopLeft = new Point(Canvas.GetLeft(this), Canvas.GetTop(this));
            Point trgPositionBottomRight = new Point(trgPositionTopLeft.X + this.ActualWidth, trgPositionTopLeft.Y + this.ActualHeight);
            Rect trgRect = new Rect(srcPositionTopLeft, srcPositionBottomRight);
            Rect transformedTrgRect = this.TransformToAncestor(this.Parent).TransformBounds(trgRect);

            if (transformedSrcRect.Contains(transformedTrgRect) ||
                transformedSrcRect.IntersectsWith(transformedTrgRect))
            {
                //drag is over element
            }
        }
    }

Remember to removed event handlers etc later.

Hope this helps someone in the future.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文