views:

35

answers:

0

Hi,

I have a custom control and and want to be able to drag the images from the control onto another control on the page.

I have mastered the drag but I cant drag the image outside the custom control. This is the code im using within the custom control

        bool isMouseCaptured;
    double mouseVerticalPosition;
    double mouseHorizontalPosition;

    public void Handle_MouseDown(object sender, MouseEventArgs args)
    {
        Canvas item = sender as Canvas;

        mouseVerticalPosition = args.GetPosition(null).Y;
        mouseHorizontalPosition = args.GetPosition(null).X;
        isMouseCaptured = true;
        item.CaptureMouse();
    }

    public void Handle_MouseMove(object sender, MouseEventArgs args)
    {
        Canvas item = sender as Canvas;

        if (isMouseCaptured)
        {

            // Calculate the current position of the object.
            double deltaV = args.GetPosition(null).Y - mouseVerticalPosition;
            double deltaH = args.GetPosition(null).X - mouseHorizontalPosition;
            double newTop = deltaV + (double)item.GetValue(Canvas.TopProperty);
            double newLeft = deltaH + (double)item.GetValue(Canvas.LeftProperty);


            // Set new position of object.
            item.SetValue(Canvas.TopProperty, newTop);
            item.SetValue(Canvas.LeftProperty, newLeft);

            // Update position global variables.
            mouseVerticalPosition = args.GetPosition(null).Y;
            mouseHorizontalPosition = args.GetPosition(null).X;
        }
    }

    public void Handle_MouseUp(object sender, MouseEventArgs args)
    {
        Canvas item = sender as Canvas;
        isMouseCaptured = false;
        item.ReleaseMouseCapture();
        mouseVerticalPosition = -1;
        mouseHorizontalPosition = -1;
    }

Any help would be appriciated Sp