Nodify学习 三:连接器

前置

连接概述

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

基本连接

库中所有连接的基类是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”将删除箭头。

连接样式

Nodify 自带3个连接器样式

  • Line connection 直线连接

  • Circuit connection 电路连接

  • Connection 贝塞尔曲线连接

Line connection 直线连接

从 Source 到 Target 的直线。

Circuit connection 电路连接

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

 

Connection 贝塞尔曲线连接

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

 

操作

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

public class NotifyPropertyBase : INotifyPropertyChanged 
{
 </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">event</span><span style="color: #000000;"> PropertyChangedEventHandler PropertyChanged;

 </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> RaisePropertyChanged([CallerMemberName] <span style="color: #0000ff;">string</span> propName = <span style="color: #800000;">""</span><span style="color: #000000;">)
 {
     PropertyChanged</span>?.Invoke(<span style="color: #0000ff;">this</span>, <span style="color: #0000ff;">new</span><span style="color: #000000;"> PropertyChangedEventArgs(propName));
 }

 </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> Set&lt;T&gt;(<span style="color: #0000ff;">ref</span> T field, T value, Action action = <span style="color: #0000ff;">null</span>, [CallerMemberName] <span style="color: #0000ff;">string</span> propName = <span style="color: #800000;">""</span><span style="color: #000000;">)
 {
     </span><span style="color: #0000ff;">if</span> (EqualityComparer&lt;T&gt;<span style="color: #000000;">.Default.Equals(field, value))
         </span><span style="color: #0000ff;">return</span><span style="color: #000000;">;

     field </span>=<span style="color: #000000;"> value;
     RaisePropertyChanged(propName);
     action</span>?<span style="color: #000000;">.Invoke();
 }</span><span style="color: #000000;">

}

 

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

 

public class ConnectionViewModel
{
    public ConnectionViewModel(ConnectorViewModel source, ConnectorViewModel target) { Source = source; Target = target; 
    Source<span class="pl-kos">.IsConnected <span class="pl-c1">= <span class="pl-c1">true<span class="pl-kos">;
    Target<span class="pl-kos">.IsConnected <span class="pl-c1">= <span class="pl-c1">true<span class="pl-kos">;
<span class="pl-kos">}

<span class="pl-k">public <span class="pl-smi">ConnectorViewModel <span class="pl-s1">Source <span class="pl-kos">{ <span class="pl-k">get<span class="pl-kos">; <span class="pl-kos">}
<span class="pl-k">public <span class="pl-smi">ConnectorViewModel <span class="pl-s1">Target <span class="pl-kos">{ <span class="pl-k">get<span class="pl-kos">; <span class="pl-kos">}

}

在EditorViewModel 类添加

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

调整ConnectorViewModel的属性

    public class ConnectorViewModel: NotifyPropertyBase
    {
        public string Title { get; set; }
    </span><span style="color: #0000ff;">private</span><span style="color: #000000;"> Point _anchor;
    </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> Point Anchor
    {
        </span><span style="color: #0000ff;">get</span> =&gt;<span style="color: #000000;"> _anchor;
        </span><span style="color: #0000ff;">set</span> =&gt; Set(<span style="color: #0000ff;">ref</span><span style="color: #000000;"> _anchor, value);
    }

    </span><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">bool</span><span style="color: #000000;"> _isConnected;
    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">bool</span><span style="color: #000000;"> IsConnected
    {
        </span><span style="color: #0000ff;">get</span> =&gt;<span style="color: #000000;"> _isConnected;
        </span><span style="color: #0000ff;">set</span> =&gt; Set(<span style="color: #0000ff;">ref</span><span style="color: #000000;"> _isConnected, value);
    }
}</span></pre>

在编辑器添加连接器样式

  <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>
              &lt;nodify:Node.OutputConnectorTemplate&gt;
                  &lt;DataTemplate DataType=<span style="color: #800000;">"</span><span style="color: #800000;">{x:Type mod:ConnectorViewModel}</span><span style="color: #800000;">"</span>&gt;
                      &lt;<span style="color: #000000;">nodify:NodeOutput
                          Anchor</span>=<span style="color: #800000;">"</span><span style="color: #800000;">{Binding Anchor, Mode=OneWayToSource}</span><span style="color: #800000;">"</span><span style="color: #000000;">
                          Header</span>=<span style="color: #800000;">"</span><span style="color: #800000;">{Binding Title}</span><span style="color: #800000;">"</span><span style="color: #000000;">
                          IsConnected</span>=<span style="color: #800000;">"</span><span style="color: #800000;">{Binding IsConnected}</span><span style="color: #800000;">"</span> /&gt;
                  &lt;/DataTemplate&gt;
              &lt;/nodify:Node.OutputConnectorTemplate&gt;
          &lt;/nodify:Node&gt;
      &lt;/DataTemplate&gt;
  &lt;/nodify:NodifyEditor.ItemTemplate&gt;

  &lt;nodify:NodifyEditor.ConnectionTemplate&gt;
      &lt;DataTemplate DataType=<span style="color: #800000;">"</span><span style="color: #800000;">{x:Type mod:ConnectionViewModel}</span><span style="color: #800000;">"</span>&gt;
          &lt;<span style="color: #000000;">nodify:Connection
              Source</span>=<span style="color: #800000;">"</span><span style="color: #800000;">{Binding Source.Anchor}</span><span style="color: #800000;">"</span><span style="color: #000000;">
              SourceOffsetMode</span>=<span style="color: #800000;">"</span><span style="color: #800000;">Rectangle</span><span style="color: #800000;">"</span><span style="color: #000000;">
              Target</span>=<span style="color: #800000;">"</span><span style="color: #800000;">{Binding Target.Anchor}</span><span style="color: #800000;">"</span><span style="color: #000000;">
              TargetOffsetMode</span>=<span style="color: #800000;">"</span><span style="color: #800000;">Rectangle</span><span style="color: #800000;">"</span> /&gt;
      &lt;/DataTemplate&gt;
  &lt;/nodify:NodifyEditor.ConnectionTemplate&gt;

</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 = "输出"
            }
        }
        };
    </span><span style="color: #0000ff;">var</span> nodify = <span style="color: #0000ff;">new</span><span style="color: #000000;"> NodeViewModel
    {
        Title </span>= <span style="color: #800000;">"</span><span style="color: #800000;">To Nodify</span><span style="color: #800000;">"</span><span style="color: #000000;">,
        Input </span>= <span style="color: #0000ff;">new</span> ObservableCollection&lt;ConnectorViewModel&gt;<span style="color: #000000;">
    {
        </span><span style="color: #0000ff;">new</span><span style="color: #000000;"> ConnectorViewModel
        {
            Title </span>= <span style="color: #800000;">"</span><span style="color: #800000;">In</span><span style="color: #800000;">"</span><span style="color: #000000;">
        }
    }
    };
    Nodes.Add(welcome);
    Nodes.Add(nodify);


    Connections.Add(</span><span style="color: #0000ff;">new</span> ConnectionViewModel(welcome.Output[<span style="color: #800080;">0</span>], nodify.Input[<span style="color: #800080;">0</span><span style="color: #000000;">]));
   
}

}

 

源码

github:zt199510/NodifySamples (github.com)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值