Nodify学习 三:连接器

1|0前置

1|1连接概述

连接是由两个点之间创建的。SourceTarget依赖属性是Point类型,通常绑定到连接器的Anchor点。

1|2基本连接

库中所有连接的基类是BaseConnection,它派生自Shape。在创建自定义连接时,可以不受任何限值地从BaseConnection派生。

它公开了两个命令及其对应的事件:

  • DisconnectCommand 及 DisconnectEvent - 当按住ALT点击连接时触发
  • SplitCommand 及 SplitEvent - 当双击连接时触发

Nodify 控件支持 Input 和 Output 连接器,您可以通过重写 InputConnectorTemplate 和 OutputConnectorTemplate 的默认模板来自定义这些连接器

Direction 的连接可以有两个值:

  • Forward

 

  • Backward

 

和 SourceOffset 与 TargetOffset 锚点一起工作 OffsetMode ,并将与锚点保持距离:

连接也有一个 Spacing ,它将使连接在距 Source 和 Target 点一定距离处断开角度:

  • With spacing: 带间距:

 

  • Without spacing: 无间距:

 设置为 ArrowSize “0,0”将删除箭头。

1|3连接样式

Nodify 自带3个连接器样式

  • Line connection 直线连接

  • Circuit connection 电路连接

  • Connection 贝塞尔曲线连接

1|0Line connection 直线连接

从 Source 到 Target 的直线。

1|0Circuit connection 电路连接

具有 Angle 依赖项属性,用于控制其中断位置。角度以度为单位。

 

1|0Connection 贝塞尔曲线连接

和 Target 之间的 Source 贝塞尔曲线。

 

2|0操作

我们先创建一个NotifyPropertyBase类 作为消息通知的基类 

public class NotifyPropertyBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged; public void RaisePropertyChanged([CallerMemberName] string propName = "") { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propName)); } public void Set<T>(ref T field, T value, Action action = null, [CallerMemberName] string propName = "") { if (EqualityComparer<T>.Default.Equals(field, value)) return; field = value; RaisePropertyChanged(propName); action?.Invoke(); } }

 

然后我们创建连接器类ConnectionViewModel  管理连接源和目标源

 

public class ConnectionViewModel { public ConnectionViewModel(ConnectorViewModel source, ConnectorViewModel target) { Source = source; Target = target; Source.IsConnected = true; Target.IsConnected = true; } public ConnectorViewModel Source { get; } public ConnectorViewModel Target { get; } }

在EditorViewModel 类添加

public ObservableCollection<ConnectionViewModel> Connections { get; } = new ObservableCollection<ConnectionViewModel>();

调整ConnectorViewModel的属性

public class ConnectorViewModel: NotifyPropertyBase { public string Title { get; set; } private Point _anchor; public Point Anchor { get => _anchor; set => Set(ref _anchor, value); } private bool _isConnected; public bool IsConnected { get => _isConnected; set => Set(ref _isConnected, value); } }

在编辑器添加连接器样式

<nodify:NodifyEditor x:Name="Editor" Background="{StaticResource GridDrawingBrush}" Connections="{Binding Connections}" ItemsSource="{Binding Nodes}"> <nodify:NodifyEditor.DataContext> <vm:EditorViewModel /> </nodify:NodifyEditor.DataContext> <nodify:NodifyEditor.ItemTemplate> <DataTemplate DataType="{x:Type mod:NodeViewModel}"> <nodify:Node Header="{Binding Title}" Input="{Binding Input}" Output="{Binding Output}"> <nodify:Node.InputConnectorTemplate> <DataTemplate DataType="{x:Type mod:ConnectorViewModel}"> <nodify:NodeInput Anchor="{Binding Anchor, Mode=OneWayToSource}" Header="{Binding Title}" IsConnected="{Binding IsConnected}" /> </DataTemplate> </nodify:Node.InputConnectorTemplate> <nodify:Node.OutputConnectorTemplate> <DataTemplate DataType="{x:Type mod:ConnectorViewModel}"> <nodify:NodeOutput Anchor="{Binding Anchor, Mode=OneWayToSource}" Header="{Binding Title}" IsConnected="{Binding IsConnected}" /> </DataTemplate> </nodify:Node.OutputConnectorTemplate> </nodify:Node> </DataTemplate> </nodify:NodifyEditor.ItemTemplate> <nodify:NodifyEditor.ConnectionTemplate> <DataTemplate DataType="{x:Type mod:ConnectionViewModel}"> <nodify:Connection Source="{Binding Source.Anchor}" SourceOffsetMode="Rectangle" Target="{Binding Target.Anchor}" TargetOffsetMode="Rectangle" /> </DataTemplate> </nodify:NodifyEditor.ConnectionTemplate> </nodify:NodifyEditor>

然后添加一个新的节点看看 连接效果 这里我用了的

Connection连接样式
public class EditorViewModel { public ObservableCollection<NodeViewModel> Nodes { get; } = new ObservableCollection<NodeViewModel>(); public ObservableCollection<ConnectionViewModel> Connections { get; } = new ObservableCollection<ConnectionViewModel>(); public EditorViewModel() { var welcome = new NodeViewModel { Title = "我的第一个节点", Input = new ObservableCollection<ConnectorViewModel> { new ConnectorViewModel { Title = "输入" } }, Output = new ObservableCollection<ConnectorViewModel> { new ConnectorViewModel { Title = "输出" } } }; var nodify = new NodeViewModel { Title = "To Nodify", Input = new ObservableCollection<ConnectorViewModel> { new ConnectorViewModel { Title = "In" } } }; Nodes.Add(welcome); Nodes.Add(nodify); Connections.Add(new ConnectionViewModel(welcome.Output[0], nodify.Input[0])); } }

 

3|0源码

github:zt199510/NodifySamples (github.com)

 


__EOF__

本文作者可乐加冰
本文链接https://www.cnblogs.com/zt199510/p/18310735.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   可乐_加冰  阅读(583)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 本地部署 DeepSeek:小白也能轻松搞定!
· 基于DeepSeek R1 满血版大模型的个人知识库,回答都源自对你专属文件的深度学习。
· 如何给本地部署的DeepSeek投喂数据,让他更懂你
· 在缓慢中沉淀,在挑战中重生!2024个人总结!
· 大人,时代变了! 赶快把自有业务的本地AI“模型”训练起来!
点击右上角即可分享
微信分享提示