Nodify学习 伍:添加移除连接控制器,设置节点初始位置
前置移除连接
要删除连接,只需监听来自连接器本身或编辑器的断开连接事件,并删除具有连接器作为源或目标的连接。为了简单起见,我们将为 NodifyEditor 实现 DisconnectConnectorCommand。首先让我们将其添加到 EditorViewModel。
public class EditorViewModel
{
public ICommand DisconnectConnectorCommand { get; }
...
public EditorViewModel()
{
DisconnectConnectorCommand = new DelegateCommand<ConnectorViewModel>(connector =>
{
var connection = Connections.First(x => x.Source == connector || x.Target == connector);
connection.Source.IsConnected = false;将连接器属性设为false
connection.Target.IsConnected = false;
Connections.Remove(connection);
});
<nodify:NodifyEditor ItemsSource="{Binding Nodes}"
Connections="{Binding Connections}"
PendingConnection="{Binding PendingConnection}"
DisconnectConnectorCommand="{Binding DisconnectConnectorCommand}">
...
<p></nodify:NodifyEditor></p>}}
Xaml
<nodify:NodifyEditor ItemsSource="{Binding Nodes}"
Connections="{Binding Connections}"
PendingConnection="{Binding PendingConnection}"
DisconnectConnectorCommand="{Binding DisconnectConnectorCommand}">
...
<p></nodify:NodifyEditor></p>
控制节点位置
如你所见,节点总是在屏幕的左上角。这是因为它们在图中的位置是 (0, 0)。让我们来改变这一点!
在 中添加一个 属性,类型为 ,并触发 事件。NodeViewModelLocationSystem.Windows.PointPropertyChanged
public class NodeViewModel : NotifyPropertyBase
{
public string Title { get; set; }
private Point _location;
public Point Location
{
get => _location;
set => Set(ref _location, value);
}
public ObservableCollection<ConnectorViewModel> Input { get; set; } = new ObservableCollection<ConnectorViewModel>();
public ObservableCollection<ConnectorViewModel> Output { get; set; } = new ObservableCollection<ConnectorViewModel>();}
Xaml
<nodify:NodifyEditor ItemsSource="{Binding Nodes}"
Connections="{Binding Connections}"
PendingConnection="{Binding PendingConnection}">
<nodify:NodifyEditor.ItemContainerStyle>
<Style TargetType="{x:Type nodify:ItemContainer}">
<Setter Property="Location"
Value="{Binding Location}" />
</Style>
</nodify:NodifyEditor.ItemContainerStyle>
...
现在你可以在构造节点时设置它们的位置。
源码
github:zt199510/NodifySamples (github.com)
来源:https://www.cnblogs.com/zt199510/p/18315740
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!
页:
[1]