DragDropTool.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. using System.Linq;
  2. using System.Windows;
  3. using SHJX.Service.WorkFlowEdit;
  4. using SHJX.Service.Common.Utils;
  5. namespace Flowchart
  6. {
  7. class DragDropTool : IDragDropTool
  8. {
  9. DiagramView _view;
  10. FlowchartModel _model;
  11. int _row, _column;
  12. public DragDropTool(DiagramView view, FlowchartModel model)
  13. {
  14. _view = view;
  15. _model = model;
  16. }
  17. #region 继承
  18. public void OnDragEnter(DragEventArgs e)
  19. {
  20. }
  21. public void OnDragLeave(DragEventArgs e)
  22. {
  23. }
  24. #endregion
  25. public void OnDragOver(DragEventArgs e)
  26. {
  27. e.Effects = DragDropEffects.None;
  28. if (e.Data.GetDataPresent(typeof(NodeKinds)))
  29. {
  30. var position = e.GetPosition(_view);
  31. _column = (int)(position.X / _view.GridCellSize.Width);
  32. _row = (int)(position.Y / _view.GridCellSize.Height);
  33. if (_column >= 0 && _row >= 0)
  34. if (!_model.Nodes.Where(p => p.Column.Equals(_column) && p.Row.Equals(_row)).Any())
  35. e.Effects = e.AllowedEffects;
  36. }
  37. e.Handled = true;
  38. }
  39. public void OnDrop(DragEventArgs e)
  40. {
  41. var node = new FlowNode((NodeKinds)e.Data.GetData(typeof(NodeKinds)))
  42. {
  43. Row = _row,
  44. Column = _column
  45. };
  46. if (node.Kind.BeIn(NodeKinds.Start, NodeKinds.End))
  47. {
  48. if (_model.Nodes.Any(item => item.Kind.Equals(node.Kind)))
  49. {
  50. e.Handled = false;
  51. return;
  52. }
  53. }
  54. _model.Nodes.Add(node);
  55. e.Handled = true;
  56. }
  57. }
  58. }