Articles in this section
Category / Section

How to restrict Connector's source/target changing from native Nodes to other Nodes in WPF Diagram(SfDiagram)?

1 min read

WPF Diagram (SfDiagram) supports restricting connector’s source and target changing from native nodes to other nodes. You can achieve your requirement by using ConnectorSourceChangedCommand/ConnectorSourceChangedEvent and ConnectorTargetChangedCommand/ConnectorTargetChangedEvent. When you are trying to change the source and the target nodes of a connector from its native nodes to any other nodes, then source and target nodes will be reset to the same source and target nodes using event arguments.

XAML

<!--Initialize the Sfdiagram-->
<syncfusion:SfDiagram x:Name="diagram"
                      Nodes="{Binding Nodes}"
                      Connectors="{Binding Connectors}"
                      ConnectorSourceChangedCommand="{Binding ConnectorSourceChangedCommand}"
                      ConnectorTargetChangedCommand="{Binding ConnectorTargetChangedCommand}">
</syncfusion:SfDiagram>

 

C# 

//Adding Commands
ConnectorSourceChangedCommand = new DelegateCommand(OnConnectorSourceChangedCommand);
ConnectorTargetChangedCommand = new DelegateCommand(OnConnectorTargetChangedCommand);
 
//Method to execute ConnectorSourceChangedCommand.
private void OnConnectorSourceChangedCommand(object parameter)
{
    var args = parameter as ChangeEventArgs<object, ConnectorChangedEventArgs>;
    if (args.NewValue.Node == null && args.OldValue.Node != null)
    {
        NodeViewModel node = args.OldValue.Node as NodeViewModel;
        //Reset source node when it is changed to null from source node.
        (args.Item as ConnectorViewModel).SourceNode = node;
    }
}
 
//Method to execute ConnectorTargetChangedCommand.
private void OnConnectorTargetChangedCommand(object parameter)
{
    var args = parameter as ChangeEventArgs<object, ConnectorChangedEventArgs>;
    if (args.NewValue.Node == null && args.OldValue.Node != null)
    {
        NodeViewModel node = args.OldValue.Node as NodeViewModel;
        //Reset target node when it is changed to null from target node.
        (args.Item as ConnectorViewModel).TargetNode = node;
    }
}

 

View sample in GitHub

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied