前置
预先连接
可以从连接器创建预先连接,并可以放置在ItemContainer
或Connector
上(如果AllowOnlyConnectors
为false)。
预先连接的Content
可以使用ContentTemplate
进行自定义。如果EnablePreview
为true,PreviewTarget
将更新为鼠标光标下的连接器或项目容器,或者为null
(如果没有这样的元素)
预先连接的可见性可以使用IsVisible
依赖属性进行控制。
连接器的连接捕捉可以使用EnableSnapping
依赖属性启用。
Source
和Target
属性是连接器的数据上下文,预先连接完成时Target
将更新。
还有一个StartedCommand
,参数是Source
,以及一个CompletedCommand
,参数是Target
。
提示:取消预先连接的方法是释放右键。
预先连接从一个 Source
开始,当放置到一个 Target
上时将完成。源始终是一个连接器,目标可以是一个连接器、一个项目容器或 null
。我们现在只关心其他连接器。当连接开始时,执行 StartedCommand
,该命令接收 Source
作为参数。当连接完成时,执行 CompletedCommand
,该命令接收 Target
作为参数。
操作
首先我们需要创建预先连接的视图模型类,并将其添加到 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>
这就是创建连接的全部内容。现在你应该可以在连接器之间创建连接了。
代码地址
Github(NodifySamples4):zt199510/NodifySamples (github.com)
1.本站内容仅供参考,不作为任何法律依据。用户在使用本站内容时,应自行判断其真实性、准确性和完整性,并承担相应风险。
2.本站部分内容来源于互联网,仅用于交流学习研究知识,若侵犯了您的合法权益,请及时邮件或站内私信与本站联系,我们将尽快予以处理。
3.本文采用知识共享 署名4.0国际许可协议 [BY-NC-SA] 进行授权
4.根据《计算机软件保护条例》第十七条规定“为了学习和研究软件内含的设计思想和原理,通过安装、显示、传输或者存储软件等方式使用软件的,可以不经软件著作权人许可,不向其支付报酬。”您需知晓本站所有内容资源均来源于网络,仅供用户交流学习与研究使用,版权归属原版权方所有,版权争议与本站无关,用户本人下载后不能用作商业或非法用途,需在24个小时之内从您的电脑中彻底删除上述内容,否则后果均由用户承担责任;如果您访问和下载此文件,表示您同意只将此文件用于参考、学习而非其他用途,否则一切后果请您自行承担,如果您喜欢该程序,请支持正版软件,购买注册,得到更好的正版服务。
5.本站是非经营性个人站点,所有软件信息均来自网络,所有资源仅供学习参考研究目的,并不贩卖软件,不存在任何商业目的及用途
暂无评论内容