This is a migrated thread and some comments may be shown as answers.

Remove connections when removing node?

8 Answers 232 Views
Diagram
This is a migrated thread and some comments may be shown as answers.
Kristoffer
Top achievements
Rank 1
Kristoffer asked on 04 Jun 2013, 12:05 PM
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?

8 Answers, 1 is accepted

Sort by
0
Petar Mladenov
Telerik team
answered on 04 Jun 2013, 02:53 PM
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.

0
Kristoffer
Top achievements
Rank 1
answered on 04 Jun 2013, 03:48 PM
Thanks. A workaround is always nice. Will you fix the bug in Q2?
0
Petar Mladenov
Telerik team
answered on 05 Jun 2013, 07:12 AM
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.

0
Kristoffer
Top achievements
Rank 1
answered on 12 Jun 2013, 12:11 PM
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?
0
Petar Mladenov
Telerik team
answered on 13 Jun 2013, 02:00 PM
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.

0
Petar Mladenov
Telerik team
answered on 03 Jul 2013, 02:10 PM
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 >>
0
Kristoffer
Top achievements
Rank 1
answered on 11 Sep 2013, 08:39 AM
The code you provided does work, but it doesn't properly handle "CanExecute".

How do you override CanExecute for this Delete-command hack?
0
Petar Mladenov
Telerik team
answered on 16 Sep 2013, 06:29 AM
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 >>
Tags
Diagram
Asked by
Kristoffer
Top achievements
Rank 1
Answers by
Petar Mladenov
Telerik team
Kristoffer
Top achievements
Rank 1
Share this question
or