开发者

How to find if Mouse is already Captured

开发者 https://www.devze.com 2023-03-31 14:56 出处:网络
I created a usercontrol which contains a Canvas and a Button on the Canvas. To be able to move the button over the canvas I attached a DragBehavior shown below:

I created a usercontrol which contains a Canvas and a Button on the Canvas. To be able to move the button over the canvas I attached a DragBehavior shown below:

class DragOverCanvasBehavior : Behavior<FrameworkElement>
{
    private Point _mouseStartPosition;

    protected override void OnAttached()
    {

        AssociatedObject.MouseLeftButtonDown += (sender, e) =>
                                                {
                                                    _mouseStartPosition =                                                            e.GetPosition((Canvas) AssociatedObject.Parent);
                                                    AssociatedObject.CaptureMouse();
                                                };

        AssociatedObject.MouseLeftButtonUp += (sender, e) => AssociatedObject.ReleaseMouseCapture();

        AssociatedObject.MouseMove += (sender, e) =>
        {
            var point = e.GetPosition((Canvas)AssociatedObject.Parent) - _mouseStartPosition;
            if (AssociatedObject.IsMouseCaptured)
            {
                Canvas.SetTop(AssociatedObject, point.Y);
                Canvas.SetLeft(AssociatedObject, point.X);
            }
        };
    }
}  

For a business reason I need to move the canvas also if I hold and drag the canvas. I did it with a similar Behavior class which changes the Canvas margins and gives it the look as if it's moving along with mouse pointer on drag. This Behavior also captures mouse.

Now the pro开发者_Go百科blem is - If I click and drag the Button, the Canvas also gets Mouse events and starts getting dragged. How can I make sure that when I am dragging the Button the Canvas Behavior does not get mouse events.

I tried putting e.Handled = true in Behaviors but that did not work.

If I can find out that the Mouse is already Captured bu some other object, I can put condition in Behavior to not to capture it again.


Test for Mouse.Captured!= null.

0

精彩评论

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