PageRenderTime 47ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

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

#
C# | 138 lines | 107 code | 31 blank | 0 comment | 10 complexity | cf6924e14fad6677328a9f8d54ef87cd MD5 | raw file
  1. using System;
  2. using System.Windows;
  3. using System.Windows.Media;
  4. using System.Windows.Controls.Primitives;
  5. using System.Windows.Controls;
  6. using System.Windows.Input;
  7. namespace EasySL.Share
  8. {
  9. public static class DragHelper
  10. {
  11. public static void Drag(this FrameworkElement attachedElement)
  12. {
  13. bool isDragging = false;
  14. Point lastPosition = new Point(0, 0);
  15. attachedElement.MouseLeftButtonDown += (s, e) =>
  16. {
  17. isDragging = true;
  18. lastPosition = e.GetPosition(null);
  19. attachedElement.CaptureMouse();
  20. };
  21. attachedElement.MouseLeftButtonUp += (s, e) =>
  22. {
  23. isDragging = false;
  24. attachedElement.ReleaseMouseCapture();
  25. };
  26. attachedElement.MouseMove += (s, e) =>
  27. {
  28. if (!isDragging)
  29. return;
  30. Point currentPosition = e.GetPosition(null);
  31. double dX = currentPosition.X - lastPosition.X;
  32. double dY = currentPosition.Y - lastPosition.Y;
  33. lastPosition = currentPosition;
  34. Transform oldTransform = attachedElement.RenderTransform;
  35. TransformGroup rt = new TransformGroup();
  36. TranslateTransform newPos = new TranslateTransform();
  37. newPos.X = dX;
  38. newPos.Y = dY;
  39. if (oldTransform != null)
  40. {
  41. rt.Children.Add(oldTransform);
  42. }
  43. rt.Children.Add(newPos);
  44. MatrixTransform mt = new MatrixTransform();
  45. mt.Matrix = rt.Value;
  46. attachedElement.RenderTransform = mt;
  47. };
  48. }
  49. public static void Drag(this FrameworkElement attachedElement, object dragObject)
  50. {
  51. attachedElement.Drag(dragObject, null, null);
  52. }
  53. public static void Drag(this FrameworkElement attachedElement, object dragObject, MouseEventHandler onMove, MouseButtonEventHandler onDragCompleted)
  54. {
  55. Popup rootPopup = new Popup();
  56. rootPopup.IsOpen = false;
  57. ContentControl dragElement = new ContentControl();
  58. dragElement.Content = dragObject;
  59. rootPopup.Child = dragElement;
  60. bool isDragging = false;
  61. Point lastPosition = new Point(0, 0);
  62. attachedElement.MouseLeftButtonDown += (s, e) =>
  63. {
  64. isDragging = true;
  65. lastPosition = e.GetPosition(null);
  66. dragElement.CaptureMouse();
  67. Point p = e.GetPosition(attachedElement);
  68. rootPopup.IsOpen = true;
  69. rootPopup.HorizontalOffset = lastPosition.X - p.X;
  70. rootPopup.VerticalOffset = lastPosition.Y - p.Y;
  71. };
  72. dragElement.MouseLeftButtonUp += (s, e) =>
  73. {
  74. isDragging = false;
  75. dragElement.ReleaseMouseCapture();
  76. rootPopup.IsOpen = false;
  77. dragElement.RenderTransform = null;
  78. if (onDragCompleted != null)
  79. onDragCompleted(s, e);
  80. };
  81. dragElement.MouseMove += (s, e) =>
  82. {
  83. if (!isDragging)
  84. return;
  85. Point currentPosition = e.GetPosition(null);
  86. double dX = currentPosition.X - lastPosition.X;
  87. double dY = currentPosition.Y - lastPosition.Y;
  88. lastPosition = currentPosition;
  89. Transform oldTransform = dragElement.RenderTransform;
  90. TransformGroup rt = new TransformGroup();
  91. TranslateTransform newPos = new TranslateTransform();
  92. newPos.X = dX;
  93. newPos.Y = dY;
  94. if (oldTransform != null)
  95. {
  96. rt.Children.Add(oldTransform);
  97. }
  98. rt.Children.Add(newPos);
  99. MatrixTransform mt = new MatrixTransform();
  100. mt.Matrix = rt.Value;
  101. dragElement.RenderTransform = mt;
  102. if (onMove != null)
  103. onMove(s, e);
  104. };
  105. }
  106. }
  107. }