Nodify学习 四:预先连接

1|0前置

1|1预先连接

可以从连接器创建预先连接,并可以放置在ItemContainerConnector上(如果AllowOnlyConnectors为false)。

预先连接的Content可以使用ContentTemplate进行自定义。如果EnablePreview为true,PreviewTarget将更新为鼠标光标下的连接器或项目容器,或者为null(如果没有这样的元素)

 

预先连接的可见性可以使用IsVisible依赖属性进行控制。

连接器的连接捕捉可以使用EnableSnapping依赖属性启用。

SourceTarget属性是连接器的数据上下文,预先连接完成时Target将更新。

还有一个StartedCommand,参数是Source,以及一个CompletedCommand,参数是Target

提示:取消预先连接的方法是释放右键。

 

 

预先连接从一个 Source 开始,当放置到一个 Target 上时将完成。源始终是一个连接器,目标可以是一个连接器、一个项目容器或 null。我们现在只关心其他连接器。当连接开始时,执行 StartedCommand,该命令接收 Source 作为参数。当连接完成时,执行 CompletedCommand,该命令接收 Target 作为参数。

2|0操作

首先我们需要创建预先连接的视图模型类,并将其添加到 EditorViewModel 中。

public class PendingConnectionViewModel : ObservableObject { private readonly EditorViewModel _editor; private ConnectorViewModel _source; public PendingConnectionViewModel(EditorViewModel editor) { _editor = editor; StartCommand = new DelegateCommand<ConnectorViewModel>(source => _source = source); FinishCommand = new DelegateCommand<ConnectorViewModel>(target => { if (target != null) _editor.Connect(_source, target); }); } public ICommand StartCommand { get; } public ICommand FinishCommand { get; } }
public class EditorViewModel { public ObservableCollection<NodeViewModel> Nodes { get; } = new ObservableCollection<NodeViewModel>(); public ObservableCollection<ConnectionViewModel> Connections { get; } = new ObservableCollection<ConnectionViewModel>(); public PendingConnectionViewModel PendingConnection { get; } public EditorViewModel() { PendingConnection = new PendingConnectionViewModel(this); var welcome = new NodeViewModel { Title = "我的第一个节点", Input = new ObservableCollection<ConnectorViewModel> { new ConnectorViewModel { Title = "输入" } }, Output = new ObservableCollection<ConnectorViewModel> { new ConnectorViewModel { Title = "输出" } } }; var nodify = new NodeViewModel { Title = "节点1", Input = new ObservableCollection<ConnectorViewModel> { new ConnectorViewModel { Title = "输入" } } }; Nodes.Add(welcome); Nodes.Add(nodify); } public void Connect(ConnectorViewModel source, ConnectorViewModel target) { var newConnection = new ConnectionViewModel(source, target); // 检查是否已经存在相同的连接 if (!Connections.Contains(newConnection)) { Connections.Add(newConnection); } } }

 

<nodify:NodifyEditor PendingConnection="{Binding PendingConnection}"> ... <nodify:NodifyEditor.PendingConnectionTemplate> <DataTemplate DataType="{x:Type local:PendingConnectionViewModel}"> <nodify:PendingConnection StartedCommand="{Binding StartCommand}" CompletedCommand="{Binding FinishCommand}" AllowOnlyConnectors="True" /> </DataTemplate> </nodify:NodifyEditor.PendingConnectionTemplate> ... </nodify:NodifyEditor>

 这就是创建连接的全部内容。现在你应该可以在连接器之间创建连接了。

3|0代码地址

Github(NodifySamples4):zt199510/NodifySamples (github.com) 

 


__EOF__

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