NodeControl.cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. using System.Windows;
  2. using System.Windows.Controls;
  3. using System.Windows.Media;
  4. using System.Windows.Shapes;
  5. using TeamAAS.FlowEditor.Models;
  6. namespace TeamAAS.FlowEditor.Controls
  7. {
  8. /// <summary>
  9. /// 节点控件
  10. /// </summary>
  11. public class NodeControl : System.Windows.Controls.Control
  12. {
  13. public const double DefaultWidth = 160;
  14. public const double DefaultHeight = 42;
  15. static NodeControl()
  16. {
  17. DefaultStyleKeyProperty.OverrideMetadata(typeof(NodeControl),
  18. new FrameworkPropertyMetadata(typeof(NodeControl)));
  19. }
  20. /// <summary>
  21. /// 获取绑定的 FlowNode
  22. /// </summary>
  23. public FlowNode GetNode()
  24. {
  25. return DataContext as FlowNode;
  26. }
  27. private Border _rectNode;
  28. private Border _loopNode;
  29. private Border _groupMainBorder;
  30. private Border _endMainBorder;
  31. private Border _exceptionBranchNode;
  32. private Path _diamondPath;
  33. public override void OnApplyTemplate()
  34. {
  35. base.OnApplyTemplate();
  36. _rectNode = GetTemplateChild("RectNode") as Border;
  37. _loopNode = GetTemplateChild("LoopNode") as Border;
  38. _groupMainBorder = GetTemplateChild("GroupMainBorder") as Border;
  39. _endMainBorder = GetTemplateChild("EndMainBorder") as Border;
  40. _exceptionBranchNode = GetTemplateChild("ExceptionBranchNode") as Border;
  41. _diamondPath = GetTemplateChild("DiamondPath") as Path;
  42. UpdateDisabledVisual();
  43. }
  44. /// <summary>
  45. /// 根据 IsEnabled 更新禁用视觉(仅中间文本背景变灰,边框/色条/文字不变)
  46. /// </summary>
  47. public void UpdateDisabledVisual()
  48. {
  49. var node = GetNode();
  50. bool disabled = node == null || !node.IsEnabled;
  51. string key = disabled ? "SecondaryTextBrush" : "RegionBrush";
  52. if (_rectNode != null) _rectNode.SetResourceReference(BackgroundProperty, key);
  53. if (_loopNode != null) _loopNode.SetResourceReference(BackgroundProperty, key);
  54. if (_groupMainBorder != null) _groupMainBorder.SetResourceReference(BackgroundProperty, key);
  55. if (_endMainBorder != null) _endMainBorder.SetResourceReference(BackgroundProperty, key);
  56. if (_exceptionBranchNode != null) _exceptionBranchNode.SetResourceReference(BackgroundProperty, key);
  57. if (_diamondPath != null) _diamondPath.SetResourceReference(Path.FillProperty, key);
  58. }
  59. }
  60. }