DragAdorner.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using System.Windows;
  2. using System.Windows.Input;
  3. using System.Windows.Documents;
  4. namespace SHJX.Service.WorkFlowEdit
  5. {
  6. public abstract class DragAdorner : Adorner
  7. {
  8. public DiagramView View { get; private set; }
  9. protected bool DoCommit { get; set; }
  10. private bool CanDrop { get; set; }
  11. protected Point Start { get; set; }
  12. protected Point End { get; set; }
  13. protected DragAdorner(DiagramView view, Point start) : base(view)
  14. {
  15. View = view;
  16. End = Start = start;
  17. this.Loaded += OnLoaded;
  18. }
  19. private void OnLoaded(object sender, RoutedEventArgs e)
  20. {
  21. DoCommit = false;
  22. CaptureMouse();
  23. }
  24. protected override void OnMouseMove(System.Windows.Input.MouseEventArgs e)
  25. {
  26. End = e.GetPosition(View);
  27. CanDrop = DoDrag();
  28. Mouse.OverrideCursor = CanDrop ? Cursor : Cursors.No;
  29. }
  30. protected override void OnMouseUp(System.Windows.Input.MouseButtonEventArgs e)
  31. {
  32. if (this.IsMouseCaptured)
  33. {
  34. DoCommit = CanDrop;
  35. this.ReleaseMouseCapture();
  36. }
  37. }
  38. protected override void OnLostMouseCapture(MouseEventArgs e)
  39. {
  40. View.DragAdorner = null;
  41. Mouse.OverrideCursor = null;
  42. EndDrag();
  43. }
  44. /// <summary>
  45. /// Returns true if drop is possible at this location
  46. /// </summary>
  47. protected abstract bool DoDrag();
  48. protected abstract void EndDrag();
  49. }
  50. }