Nodify学习 伍:添加移除连接控制器,设置节点初始位置

前置

移除连接

要删除连接,只需监听来自连接器本身或编辑器的断开连接事件,并删除具有连接器作为源或目标的连接。为了简单起见,我们将为 NodifyEditor 实现 DisconnectConnectorCommand。首先让我们将其添加到 EditorViewModel

 

public class EditorViewModel
{
    public ICommand DisconnectConnectorCommand { get; }
...

</span><span style="color: #0000ff;">public</span><span style="color: #000000;"> EditorViewModel()
{
    DisconnectConnectorCommand </span>= <span style="color: #0000ff;">new</span> DelegateCommand&lt;ConnectorViewModel&gt;(connector =&gt;<span style="color: #000000;">
    {
        </span><span style="color: #0000ff;">var</span> connection = Connections.First(x =&gt; x.Source == connector || x.Target ==<span style="color: #000000;"> connector);
        connection.Source.IsConnected </span>= <span style="color: #0000ff;">false</span>;  将连接器属性设为false
        connection.Target.IsConnected = <span style="color: #0000ff;">false</span><span style="color: #000000;">;
        Connections.Remove(connection);
    });

    ...
}

}

Xaml

<nodify:NodifyEditor ItemsSource="{Binding Nodes}"
                     Connections="{Binding Connections}"
                     PendingConnection="{Binding PendingConnection}"
                     DisconnectConnectorCommand="{Binding DisconnectConnectorCommand}">
  ...

</nodify:NodifyEditor>

 

控制节点位置

如你所见,节点总是在屏幕的左上角。这是因为它们在图中的位置是 (0, 0)。让我们来改变这一点!

在 中添加一个 属性,类型为 ,并触发 事件。NodeViewModelLocationSystem.Windows.PointPropertyChanged

 public class NodeViewModel : NotifyPropertyBase
 {
     public string Title { get; set; }
 </span><span style="color: #0000ff;">private</span><span style="color: #000000;"> Point _location;
 </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> Point Location
 {
     </span><span style="color: #0000ff;">get</span> =&gt;<span style="color: #000000;"> _location;
     </span><span style="color: #0000ff;">set</span> =&gt; Set(<span style="color: #0000ff;">ref</span><span style="color: #000000;"> _location, value);
 }
 </span><span style="color: #0000ff;">public</span> ObservableCollection&lt;ConnectorViewModel&gt; Input { <span style="color: #0000ff;">get</span>; <span style="color: #0000ff;">set</span>; } = <span style="color: #0000ff;">new</span> ObservableCollection&lt;ConnectorViewModel&gt;<span style="color: #000000;">();
 </span><span style="color: #0000ff;">public</span> ObservableCollection&lt;ConnectorViewModel&gt; Output { <span style="color: #0000ff;">get</span>; <span style="color: #0000ff;">set</span>; } = <span style="color: #0000ff;">new</span> ObservableCollection&lt;ConnectorViewModel&gt;<span style="color: #000000;">();

}

 

Xaml

<nodify:NodifyEditor ItemsSource="{Binding Nodes}"
                     Connections="{Binding Connections}"
                     PendingConnection="{Binding PendingConnection}">
&lt;nodify:NodifyEditor.ItemContainerStyle&gt;
    &lt;Style TargetType=<span style="color: #800000;">"</span><span style="color: #800000;">{x:Type nodify:ItemContainer}</span><span style="color: #800000;">"</span>&gt;
        &lt;Setter Property=<span style="color: #800000;">"</span><span style="color: #800000;">Location</span><span style="color: #800000;">"</span><span style="color: #000000;"> 
                Value</span>=<span style="color: #800000;">"</span><span style="color: #800000;">{Binding Location}</span><span style="color: #800000;">"</span> /&gt;
    &lt;/Style&gt;
&lt;/nodify:NodifyEditor.ItemContainerStyle&gt;<span style="color: #000000;">

...

</nodify:NodifyEditor>

现在你可以在构造节点时设置它们的位置。

源码

github:zt199510/NodifySamples (github.com)

posted @ 2024-07-24 08:51  可乐_加冰  阅读(199)  评论(1编辑  收藏  举报