EasySL /1.0/EasySL.Share.SL/DragHelper.cs

Language C# Lines 139
MD5 Hash cf6924e14fad6677328a9f8d54ef87cd Estimated Cost $2,585 (why?)
Repository https://EasySL.svn.codeplex.com/svn View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
using System;
using System.Windows;
using System.Windows.Media;
using System.Windows.Controls.Primitives;
using System.Windows.Controls;
using System.Windows.Input;

namespace EasySL.Share
{
    public static class DragHelper
    {
        public static void Drag(this FrameworkElement attachedElement)
        {
            bool isDragging = false;
            Point lastPosition = new Point(0, 0);

            attachedElement.MouseLeftButtonDown += (s, e) =>
            {
                isDragging = true;
                lastPosition = e.GetPosition(null);
                attachedElement.CaptureMouse();
            };

            attachedElement.MouseLeftButtonUp += (s, e) =>
            {
                isDragging = false;
                attachedElement.ReleaseMouseCapture();
            };

            attachedElement.MouseMove += (s, e) =>
            {
                if (!isDragging)
                    return;

                Point currentPosition = e.GetPosition(null);

                double dX = currentPosition.X - lastPosition.X;
                double dY = currentPosition.Y - lastPosition.Y;

                lastPosition = currentPosition;

                Transform oldTransform = attachedElement.RenderTransform;
                TransformGroup rt = new TransformGroup();
                TranslateTransform newPos = new TranslateTransform();
                newPos.X = dX;
                newPos.Y = dY;

                if (oldTransform != null)
                {
                    rt.Children.Add(oldTransform);
                }
                rt.Children.Add(newPos);

                MatrixTransform mt = new MatrixTransform();
                mt.Matrix = rt.Value;

                attachedElement.RenderTransform = mt;

            };
        }

        public static void Drag(this FrameworkElement attachedElement, object dragObject)
        {
            attachedElement.Drag(dragObject, null, null);
        }

        public static void Drag(this FrameworkElement attachedElement, object dragObject, MouseEventHandler onMove, MouseButtonEventHandler onDragCompleted)
        {
            Popup rootPopup = new Popup();
            rootPopup.IsOpen = false;

            ContentControl dragElement = new ContentControl();
            dragElement.Content = dragObject;

            rootPopup.Child = dragElement;

            bool isDragging = false;
            Point lastPosition = new Point(0, 0);

            attachedElement.MouseLeftButtonDown += (s, e) =>
            {
                isDragging = true;
                lastPosition = e.GetPosition(null);
                dragElement.CaptureMouse();

                Point p = e.GetPosition(attachedElement);
                rootPopup.IsOpen = true;
                rootPopup.HorizontalOffset = lastPosition.X - p.X;
                rootPopup.VerticalOffset = lastPosition.Y - p.Y;
            };

            dragElement.MouseLeftButtonUp += (s, e) =>
            {
                isDragging = false;
                dragElement.ReleaseMouseCapture();

                rootPopup.IsOpen = false;
                dragElement.RenderTransform = null;

                if (onDragCompleted != null)
                    onDragCompleted(s, e);
            };

            dragElement.MouseMove += (s, e) =>
            {
                if (!isDragging)
                    return;

                Point currentPosition = e.GetPosition(null);

                double dX = currentPosition.X - lastPosition.X;
                double dY = currentPosition.Y - lastPosition.Y;

                lastPosition = currentPosition;

                Transform oldTransform = dragElement.RenderTransform;
                TransformGroup rt = new TransformGroup();
                TranslateTransform newPos = new TranslateTransform();
                newPos.X = dX;
                newPos.Y = dY;

                if (oldTransform != null)
                {
                    rt.Children.Add(oldTransform);
                }
                rt.Children.Add(newPos);

                MatrixTransform mt = new MatrixTransform();
                mt.Matrix = rt.Value;

                dragElement.RenderTransform = mt;

                if (onMove != null)
                    onMove(s, e);
            };
        }
    }
}
Back to Top