开发者

Wpf Thumb Drag not firing drag enter on other component

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 w开发者_运维问答ork?


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜