Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: WPF
public static class DragBehv
    {
        //private static Point mouseStartPosition = new Point();
       // public static Point deltaChange = new Point();
        public static bool isDown = false;
        private static string abc = "";
 
        static Point selectedOrigins;
        static Point startPoint;
        static UIElement selectedElement;
 
        public static readonly DependencyProperty DragMotion =
           DependencyProperty.RegisterAttached("DragMotion",
               typeof(bool),
               typeof(DragBehv),
               new UIPropertyMetadata(false, MouseDragPropertyClick)
            );
 
        public static void MouseDragPropertyClick(DependencyObject dpo, DependencyPropertyChangedEventArgs args)
        {
           if (dpo is UIElement)
            {
                Canvas Cntrl = dpo as Canvas;
                Cntrl.PreviewMouseMove += new MouseEventHandler(element_MouseMove);
                Cntrl.PreviewMouseLeftButtonDown += new MouseButtonEventHandler(Mouse_LeftDownClick);
                Cntrl.PreviewMouseLeftButtonUp += new MouseButtonEventHandler(Mouse_LeftUpClick);
 
            }
        }
 
        public static void element_MouseMove(object sender, MouseEventArgs e)
        {
            if (((Canvas)sender).IsMouseCaptured)
            {
                Point CurrPosition = e.GetPosition((Canvas)sender);
                CurrPosition.X = Math.Abs(CurrPosition.X - startPoint.X);
                CurrPosition.Y = Math.Abs(CurrPosition.Y - startPoint.Y);
 
                if (isDown && Convert.ToBoolean(CurrPosition.X) && Convert.ToBoolean(CurrPosition.Y))  
                {
                    double elementLeft = CurrPosition.X + selectedOrigins.X;
                    double elementTop = CurrPosition.Y + selectedOrigins.Y;
                           
                     Canvas.SetLeft(selectedElement, elementLeft);
                     Canvas.SetTop(selectedElement, elementTop);
                 }
            }
           }
 
        public static void Mouse_LeftDownClick(object sender, MouseEventArgs e)
        {
            if (e.Source is Canvas)
                return;
 
            if (!isDown)
            {
                isDown = true;
                startPoint = e.GetPosition((Canvas)sender);
                selectedElement = e.Source as Button;
                selectedOrigins = Mouse.GetPosition(selectedElement);
                ((Canvas)sender).CaptureMouse();
            }
            e.Handled = true;
        }
 
        public static void Mouse_LeftUpClick(object sender, MouseEventArgs e)
        {
            abc += "In Left Up";
            if (((Canvas)sender).IsMouseCaptured)
              {
                  isDown = false;
                  ((Canvas)sender).ReleaseMouseCapture();
                  e.Handled = true;
              }
 

        }
 
        public static bool GetDragMotion(DependencyObject obj)
        {
            return (bool)obj.GetValue(DragMotion);
        }
 
        public static void SetDragMotion(DependencyObject obj, bool value)
        {
            obj.SetValue(DragMotion, value);
        }
 
    }
Posted 10-Jan-13 5:19am
Edited 10-Jan-13 5:21am
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You're taking the absolute value of the change in position for X and Y
CurrPosition.X = Math.Abs(CurrPosition.X - startPoint.X);
CurrPosition.Y = Math.Abs(CurrPosition.Y - startPoint.Y);

Forget about the Y direction for a minute and think about just moving the mouse to the left by one pixel. What do you think the value of (CurrPosition.X - startPoint.X) will be in this instance? Then think about what value you are adding to the X position of the element. Let me know how you go.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Thanks for your points, (CurrPosition.X - startPoint.X) should return -1 for one pixel movement by left. 
i have done by just considering the final mouse position and subtracting the initial element origin wrt mouse when mouse down was invoked. As
CurrPosition.X = CurrPosition.X - selectedOrigins.X;
And removed Math.Abs()
  Permalink  
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 277
1 Maciej Los 240
2 DamithSL 200
3 Raul Iloc 185
4 Richard MacCutchan 175
0 OriginalGriff 5,130
1 DamithSL 4,197
2 Maciej Los 3,670
3 Kornfeld Eliyahu Peter 3,470
4 Sergey Alexandrovich Kryukov 2,821


Advertise | Privacy | Mobile
Web01 | 2.8.141216.1 | Last Updated 11 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100