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
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();
}
Petar Mladenov
Telerik
Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.
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.
Petar Mladenov
Telerik
Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.
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.
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
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 >>
How do you override CanExecute for this Delete-command hack?
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();
}
Petar Mladenov
Telerik
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 >>