using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;
using TeamAAS.FlowEditor.Models;
namespace TeamAAS.FlowEditor.Controls
{
///
/// 节点控件
///
public class NodeControl : System.Windows.Controls.Control
{
public const double DefaultWidth = 160;
public const double DefaultHeight = 42;
static NodeControl()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(NodeControl),
new FrameworkPropertyMetadata(typeof(NodeControl)));
}
///
/// 获取绑定的 FlowNode
///
public FlowNode GetNode()
{
return DataContext as FlowNode;
}
private Border _rectNode;
private Border _loopNode;
private Border _groupMainBorder;
private Border _endMainBorder;
private Border _exceptionBranchNode;
private Path _diamondPath;
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
_rectNode = GetTemplateChild("RectNode") as Border;
_loopNode = GetTemplateChild("LoopNode") as Border;
_groupMainBorder = GetTemplateChild("GroupMainBorder") as Border;
_endMainBorder = GetTemplateChild("EndMainBorder") as Border;
_exceptionBranchNode = GetTemplateChild("ExceptionBranchNode") as Border;
_diamondPath = GetTemplateChild("DiamondPath") as Path;
UpdateDisabledVisual();
}
///
/// 根据 IsEnabled 更新禁用视觉(仅中间文本背景变灰,边框/色条/文字不变)
///
public void UpdateDisabledVisual()
{
var node = GetNode();
bool disabled = node == null || !node.IsEnabled;
string key = disabled ? "SecondaryTextBrush" : "RegionBrush";
if (_rectNode != null) _rectNode.SetResourceReference(BackgroundProperty, key);
if (_loopNode != null) _loopNode.SetResourceReference(BackgroundProperty, key);
if (_groupMainBorder != null) _groupMainBorder.SetResourceReference(BackgroundProperty, key);
if (_endMainBorder != null) _endMainBorder.SetResourceReference(BackgroundProperty, key);
if (_exceptionBranchNode != null) _exceptionBranchNode.SetResourceReference(BackgroundProperty, key);
if (_diamondPath != null) _diamondPath.SetResourceReference(Path.FillProperty, key);
}
}
}