DragDropTool.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. using Aga.Diagrams;
  2. using Aga.Diagrams.Tools;
  3. using System.Linq;
  4. using System.Windows;
  5. namespace FlowWorkEditDemo
  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. public void OnDragEnter(DragEventArgs e)
  18. {
  19. }
  20. public void OnDragOver(DragEventArgs e)
  21. {
  22. e.Effects = DragDropEffects.None;
  23. if (e.Data.GetDataPresent(typeof(NodeKinds)))
  24. {
  25. var position = e.GetPosition(_view);
  26. _column = (int)(position.X / _view.GridCellSize.Width);
  27. _row = (int)(position.Y / _view.GridCellSize.Height);
  28. if (_column >= 0 && _row >= 0)
  29. if (_model.Nodes.Where(p => p.Column == _column && p.Row == _row).Count() == 0)
  30. e.Effects = e.AllowedEffects;
  31. }
  32. e.Handled = true;
  33. }
  34. public void OnDragLeave(System.Windows.DragEventArgs e)
  35. {
  36. }
  37. public void OnDrop(System.Windows.DragEventArgs e)
  38. {
  39. var node = new FlowNode((NodeKinds)e.Data.GetData(typeof(NodeKinds)));
  40. node.Row = _row;
  41. node.Column = _column;
  42. _model.Nodes.Add(node);
  43. e.Handled = true;
  44. }
  45. }
  46. }