Remove connections when removing node?

9 posts, 0 answers
  1. Kristoffer
    Kristoffer avatar
    158 posts
    Member since:
    Dec 2012

    Posted 04 Jun 2013 Link to this post

    When I remove a node (using the DEL key) I want its connections to be removed too. As it is now, I get these orphaned connections when removing a node.

    This is the scenario:
    1) Node A is connected - through connection X - to node B.
    2) B is selected. I press the DEL key. Both B and X are removed.
    3) I press Ctrl+Z. Both the removal of B and X are undone.

    This behavior is absolutely necessary for our product. How can I accomplish this?
  2. Petar Mladenov
    Admin
    Petar Mladenov avatar
    2891 posts

    Posted 04 Jun 2013 Link to this post

    Hi Kristoffer,

     We have an property for this functionality (DiagramExtensionProperties.ShouldDetachConnections), but unfortunately it currently does not work. The way to workaround this is to create an UndoableDelegateCommand and execute it with DIagram.UndoRedoService;

    public MainWindow()
            {
                InitializeComponent();
                CommandBinding binding = new CommandBinding()
                {
                    Command = DiagramCommands.Delete
                };
                this.diagram.CommandBindings.Add(binding);
                binding.Executed += DeleteCommandExecuted;
            }
     
            private void DeleteCommandExecuted(object sender, ExecutedRoutedEventArgs e)
            {
                List<IConnection> connections = new List<IConnection>();
                List<IShape> shapes = new List<IShape>();
     
                this.diagram.SelectedItems.Where(sh => sh is RadDiagramShape).ForEach(s =>
                {
                    connections.AddRange((s as RadDiagramShape).OutgoingLinks);
                    connections.AddRange((s as RadDiagramShape).IncomingLinks);
                    shapes.Add(s as RadDiagramShape);
                });
                this.diagram.SelectedItems.Where(con => con is RadDiagramConnection).ForEach(con =>
                {
                    connections.Add(con as RadDiagramConnection);
                });
     
                Action<object> action = new Action<object>(x =>
                {
                    connections.ForEach(con => this.diagram.RemoveConnection(con));
                    shapes.ForEach(sh => this.diagram.RemoveShape(sh));
                });
     
                Action<object> undoAction = new Action<object>(x =>
                {
                    shapes.ForEach(sh => this.diagram.AddShape(sh));
                    connections.ForEach(con => this.diagram.AddConnection(con));
                });
     
                UndoableDelegateCommand deleteShapeAndConnection = new UndoableDelegateCommand("removeCommand", action, undoAction);
                this.diagram.UndoRedoService.ExecuteCommand(deleteShapeAndConnection);
                this.diagram.Focus();
            }
    This is demonstrated in the attached project as well. We hope it will help you proceed further. Regards,
    Petar Mladenov
    Telerik

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  3. UI for WPF is Visual Studio 2017 Ready
  4. Kristoffer
    Kristoffer avatar
    158 posts
    Member since:
    Dec 2012

    Posted 04 Jun 2013 Link to this post

    Thanks. A workaround is always nice. Will you fix the bug in Q2?
  5. Petar Mladenov
    Admin
    Petar Mladenov avatar
    2891 posts

    Posted 05 Jun 2013 Link to this post

    Hello Kristoffer,

    Q2 is right at the door (in a few weeks) but we will try to include this fix in the service pack next month.
    On a side note, you can check our beta release here. For detailed information about the release, you can check out this blog post.

    Regards,
    Petar Mladenov
    Telerik

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  6. Kristoffer
    Kristoffer avatar
    158 posts
    Member since:
    Dec 2012

    Posted 12 Jun 2013 Link to this post

    If we want to add a confirmation dialog for this DiagramCommands.Delete command, would we need to use similar code to what you suggested in your workaround? There is no built-in support for this?
  7. Petar Mladenov
    Admin
    Petar Mladenov avatar
    2891 posts

    Posted 13 Jun 2013 Link to this post

    Hello Kristoffer,

     If you use Q1 or Q2 , yes you will need the custom code. The other way around is to use Q2 SP which will be released next moth. The mentioned issue is scheduled for Q2 SP, so we suggest you to use the only attached property once the fix is available in Q2 SP.

    Regards,
    Petar Mladenov
    Telerik

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  8. Petar Mladenov
    Admin
    Petar Mladenov avatar
    2891 posts

    Posted 03 Jul 2013 Link to this post

    Hello Kristoffer,

     I want to apologize for misleading you previously. Actually, the purpose of the ShouldDetachConnections property when set to true is the following: when a shape A is selected among a connection connected to shape B and Drag or Rotate operation is performed, the property determines whether the connection should be detached from shape B. This means that the custom code I sent you will be the only way to achieve your requirement. We will mark the PITS item as *deleted* with a proper explanation. Please again excuse me for the inconvenience caused.

    Regards,
    Petar Mladenov
    Telerik
    TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WPF.
    Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
    Sign up for Free application insights >>
  9. Kristoffer
    Kristoffer avatar
    158 posts
    Member since:
    Dec 2012

    Posted 11 Sep 2013 Link to this post

    The code you provided does work, but it doesn't properly handle "CanExecute".

    How do you override CanExecute for this Delete-command hack?
  10. Petar Mladenov
    Admin
    Petar Mladenov avatar
    2891 posts

    Posted 16 Sep 2013 Link to this post

    Hi Kristoffer,

     You can try this approach:

    this.diagram.CommandBindings.Add(binding);
        binding.Executed += DeleteCommandExecuted;
        binding.PreviewCanExecute += binding_PreviewCanExecute;
        binding.CanExecute += binding_CanExecute;
    }
     
    void binding_CanExecute(object sender, CanExecuteRoutedEventArgs e)
    {
        throw new NotImplementedException();
    }
     
    void binding_PreviewCanExecute(object sender, CanExecuteRoutedEventArgs e)
    {
        throw new NotImplementedException();
    }

    Regards,
    Petar Mladenov
    Telerik
    TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WPF.
    Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
    Sign up for Free application insights >>
Back to Top
UI for WPF is Visual Studio 2017 Ready