第五节:Nodify 节点位置设置

引言

如果你尝试过前几节的代码,会发现节点都是出现在0,0 位置,及编辑器左上角。编辑器作为最外层的交互控件,内部封装了节点容器ItemContrainer,我们通过样式属性对Loaction做绑定。本节将介绍如何配置节点位置。

1、节点位置

需要在节点类中增加System.Windows.Point类型 位置属性

public partial class NodeViewModel : ObservableObject
{
    public string Title { get; set; }
    //节点位置
    [ObservableProperty]
    Point _location;
    public ObservableCollection<ConnectorViewModel> Input { get; set; } =
        new ObservableCollection<ConnectorViewModel>();
    public ObservableCollection<ConnectorViewModel> Output { get; set; } =
        new ObservableCollection<ConnectorViewModel>();
}

2、位置绑定

在编辑器中绑定节点位置属性

<nodify:NodifyEditor.ItemContainerStyle>
    <Style TargetType="{x:Type nodify:ItemContainer}">
        <Setter Property="Location" Value="{Binding Location}" />
    </Style>
</nodify:NodifyEditor.ItemContainerStyle>

完整xaml代码如下

 <Grid>
     <nodify:NodifyEditor
         Name="Editor"
         Connections="{Binding Connections}"
         ItemsSource="{Binding Nodes}"
         PendingConnection="{Binding PendingConnection}">
         <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>
                     <nodify:Node.OutputConnectorTemplate>
                         <DataTemplate DataType="{x:Type mod:ConnectorViewModel}">
                             <nodify:NodeInput
                                 Anchor="{Binding Anchor, Mode=OneWayToSource}"
                                 Header="{Binding Title}"
                                 IsConnected="{Binding IsConnected}" />
                         </DataTemplate>
                     </nodify:Node.OutputConnectorTemplate>
                 </nodify:Node>
             </DataTemplate>
         </nodify:NodifyEditor.ItemTemplate>
         <nodify:NodifyEditor.ConnectionTemplate>
             <DataTemplate DataType="{x:Type vm:ConnectionViewModel}">
                 <nodify:StepConnection Source="{Binding Source.Anchor}" Target="{Binding Target.Anchor}" />
             </DataTemplate>
         </nodify:NodifyEditor.ConnectionTemplate>
         <nodify:NodifyEditor.PendingConnectionTemplate>
             <DataTemplate DataType="{x:Type vm:PendingConnectionViewModel}">
                 <nodify:PendingConnection
                     AllowOnlyConnectors="True"
                     CompletedCommand="{Binding FinishCommand}"
                     StartedCommand="{Binding StartCommand}" />
             </DataTemplate>
         </nodify:NodifyEditor.PendingConnectionTemplate>
         <nodify:NodifyEditor.ItemContainerStyle>
             <Style TargetType="{x:Type nodify:ItemContainer}">
                 <Setter Property="Location" Value="{Binding Location}" />
             </Style>
         </nodify:NodifyEditor.ItemContainerStyle>
     </nodify:NodifyEditor>

 </Grid>

在编辑器构造中添加示例编辑器代码运行查看本节演示内容

 var welcome = new NodeViewModel()
 {
     Title = "我的第一个节点",
     Input = new ObservableCollection<ConnectorViewModel>
     {
         new ConnectorViewModel { Title = "输入" }
     },
     Output = new ObservableCollection<ConnectorViewModel>
     {
         new ConnectorViewModel { Title = "输出" }
     },
     Location = new Point(10, 10)
 };
 var node2 = new NodeViewModel()
 {
     Title = "我的第二个节点",
     Input = new ObservableCollection<ConnectorViewModel>
     {
         new ConnectorViewModel { Title = "输入" }
     },
     Output = new ObservableCollection<ConnectorViewModel>
     {
         new ConnectorViewModel { Title = "输出" }
     },
     Location = new Point(50, 100)
 };
 var nodify = new NodeViewModel
 {
     Title = "To Nodify",
     Input = new ObservableCollection<ConnectorViewModel>
     {
         new ConnectorViewModel { Title = "In" }
     },
     Location = new Point(200, 10)
 };
 Nodes.Add(welcome);
 Nodes.Add(node2);
 Nodes.Add(nodify);
 Connections.Add(new ConnectionViewModel(welcome.Output[0], nodify.Input[0]));

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

James.TCG

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值