前置
连接概述
连接是由两个点之间创建的。Source
和Target
依赖属性是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<T>(<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<T><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> =><span style="color: #000000;"> _anchor; </span><span style="color: #0000ff;">set</span> => 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> =><span style="color: #000000;"> _isConnected; </span><span style="color: #0000ff;">set</span> => 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><nodify:Node.OutputConnectorTemplate> <DataTemplate DataType=<span style="color: #800000;">"</span><span style="color: #800000;">{x:Type mod:ConnectorViewModel}</span><span style="color: #800000;">"</span>> <<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> /> </DataTemplate> </nodify:Node.OutputConnectorTemplate> </nodify:Node> </DataTemplate> </nodify:NodifyEditor.ItemTemplate> <nodify:NodifyEditor.ConnectionTemplate> <DataTemplate DataType=<span style="color: #800000;">"</span><span style="color: #800000;">{x:Type mod:ConnectionViewModel}</span><span style="color: #800000;">"</span>> <<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> /> </DataTemplate> </nodify:NodifyEditor.ConnectionTemplate>
</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<ConnectorViewModel><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)