Nodify学习 四:预先毗连

打印 上一主题 下一主题

主题 887|帖子 887|积分 2661

前置

预先毗连

可以从毗连器创建预先毗连,并可以放置在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 中。
  1. public class PendingConnectionViewModel : ObservableObject
  2. {
  3.     private readonly EditorViewModel _editor;
  4.     private ConnectorViewModel _source;
  5.     public PendingConnectionViewModel(EditorViewModel editor)
  6.     {
  7.         _editor = editor;
  8.         StartCommand = new DelegateCommand<ConnectorViewModel>(source => _source = source);
  9.         FinishCommand = new DelegateCommand<ConnectorViewModel>(target =>
  10.         {
  11.             if (target != null)
  12.                 _editor.Connect(_source, target);
  13.         });
  14.     }
  15.     public ICommand StartCommand { get; }
  16.     public ICommand FinishCommand { get; }
  17. }
复制代码
  1. public class EditorViewModel
  2. {
  3.      public ObservableCollection<NodeViewModel> Nodes { get; } = new ObservableCollection<NodeViewModel>();
  4.      public ObservableCollection<ConnectionViewModel> Connections { get; } = new ObservableCollection<ConnectionViewModel>();
  5.      public PendingConnectionViewModel PendingConnection { get; }
  6.      public EditorViewModel()
  7.      {
  8.          PendingConnection  = new PendingConnectionViewModel(this);
  9.          var welcome = new NodeViewModel
  10.          {
  11.              Title = "我的第一个节点",
  12.              Input = new ObservableCollection<ConnectorViewModel>
  13.          {
  14.              new ConnectorViewModel
  15.              {
  16.                  Title = "输入"
  17.              }
  18.          },
  19.              Output = new ObservableCollection<ConnectorViewModel>
  20.          {
  21.              new ConnectorViewModel
  22.              {
  23.                  Title = "输出"
  24.              }
  25.          }
  26.          };
  27.    
  28.          var nodify = new NodeViewModel
  29.          {
  30.              Title = "节点1",
  31.              Input = new ObservableCollection<ConnectorViewModel>
  32.          {
  33.              new ConnectorViewModel
  34.              {
  35.                  Title = "输入"
  36.              }
  37.          }
  38.          };
  39.          Nodes.Add(welcome);
  40.          Nodes.Add(nodify);
  41.         
  42.         
  43.      }
  44.      public void Connect(ConnectorViewModel source, ConnectorViewModel target)
  45.      {
  46.          var newConnection = new ConnectionViewModel(source, target);
  47.          // 检查是否已经存在相同的连接
  48.          if (!Connections.Contains(newConnection))
  49.          {
  50.              Connections.Add(newConnection);
  51.          }
  52.      }
  53. }
复制代码
 
  1. <nodify:NodifyEditor PendingConnection="{Binding PendingConnection}">
  2. ...
  3.     <nodify:NodifyEditor.PendingConnectionTemplate>
  4.         <DataTemplate DataType="{x:Type local:PendingConnectionViewModel}">
  5.             <nodify:PendingConnection StartedCommand="{Binding StartCommand}"
  6.                                       CompletedCommand="{Binding FinishCommand}"
  7.                                       AllowOnlyConnectors="True" />
  8.         </DataTemplate>
  9.     </nodify:NodifyEditor.PendingConnectionTemplate>
  10. ...
  11. </nodify:NodifyEditor>
复制代码
 这就是创建毗连的全部内容。现在你应该可以在毗连器之间创建毗连了。

代码地址

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

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

正序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

滴水恩情

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表