- A+
所属分类:.NET技术
前置
移除连接
要删除连接,只需监听来自连接器本身或编辑器的断开连接事件,并删除具有连接器作为源或目标的连接。为了简单起见,我们将为 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<ConnectorViewModel>(connector =><span style="color: #000000;"> { </span><span style="color: #0000ff;">var</span> connection = Connections.First(x => 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)。让我们来改变这一点!
在 中添加一个 属性,类型为 ,并触发 事件。NodeViewModel
Location
System.Windows.Point
PropertyChanged
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> =><span style="color: #000000;"> _location; </span><span style="color: #0000ff;">set</span> => Set(<span style="color: #0000ff;">ref</span><span style="color: #000000;"> _location, value); } </span><span style="color: #0000ff;">public</span> ObservableCollection<ConnectorViewModel> Input { <span style="color: #0000ff;">get</span>; <span style="color: #0000ff;">set</span>; } = <span style="color: #0000ff;">new</span> ObservableCollection<ConnectorViewModel><span style="color: #000000;">(); </span><span style="color: #0000ff;">public</span> ObservableCollection<ConnectorViewModel> Output { <span style="color: #0000ff;">get</span>; <span style="color: #0000ff;">set</span>; } = <span style="color: #0000ff;">new</span> ObservableCollection<ConnectorViewModel><span style="color: #000000;">();
}
Xaml
<nodify:NodifyEditor ItemsSource="{Binding Nodes}" Connections="{Binding Connections}" PendingConnection="{Binding PendingConnection}"><nodify:NodifyEditor.ItemContainerStyle> <Style TargetType=<span style="color: #800000;">"</span><span style="color: #800000;">{x:Type nodify:ItemContainer}</span><span style="color: #800000;">"</span>> <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> /> </Style> </nodify:NodifyEditor.ItemContainerStyle><span style="color: #000000;"> ...
</nodify:NodifyEditor>
现在你可以在构造节点时设置它们的位置。