I have created two nodes and a link between them.
I performed Undo once and handled the RemovedLink event, so the link was removed.
Now when I perform Redo, AddedLink event is called and I am trying to get the corresponding Link object by calling PartManager.FindLinkForData(), but this method return null.
How do I access the link object during AddedLink event?
What are you trying to accomplish in your AddedLink and RemovedLink events?
Normally the operation of the Diagram, DiagramModel, and UndoManager is such that you don’t have to do anything to update the model when Parts are added or removed, nor do you have to update the diagram when the model has data added or removed.
If you want to update a database when the model has changed, that’s fine. But you should let the normal processes update the diagram whenever the model has changed – the Diagram listens for model changes on the DiagramModel, so you don’t have to.
We have designed our application in such a way that when a node or link is created in goXam, we create the corresponding backend object (at Domain Layer). To achieve this, we listen to events like AddedNode, RemovedNode, AddedLink and RemovedLink.
If a link is created during undo/redo, i need to create the corresponding backend object. For which i need to handle the AddedLink event. We need access to the newly created link object, to get the port information and link route.
Is there any alrenative way to handle the creation of link during undo/redo?
OK, that makes sense, but I don’t understand why when you have the link data object (that is held in the GraphLinksModel.LinksSource), why do you need access to the corresponding Link? It might not exist yet, or it might be incomplete.
We store custom information in the port, to get the port object we need to access the link object.
We faced a similar situation while creating link, but we found a workaround to handle it. i.e Instead of handling the AddedLink event we handle CommittedTransaction event which will have the AddedLink part of the edits. Link object was available after the transaction was committed.
In undo/redo case, the FinishedUndo’s edits has RemovedLink not AddedLink.
I would think that the port information would be in the model, so again you would not need to access any particular Link or Node.
Do you realize that whenever any Node or LInk or any other Diagram code wants to know about any relationships, it asks the model?
But despite that, why can’t you use the same technique as for adding a link? Only handle “Transaction” ChangeEvents, and scan the list of changes in the transaction to figure out what happened. Both undo and redo just iterate over the same list, although obviously in different directions.
Unfortunately, In our design the port information is actually defined in the XAML, so we need the Link object to access the port information.
During undo/redo operations “Transaction” event is not raised. Only the StartingUndo/StaringRedo and FinishedUndo/FinishedRedo events are raised. These event contain a list of edits, but they don’t actually represent the operations done as part of undo/redo but contain the actual operation.
Eg. When i add a node to the diagram, as we know the AddedNode event is raised.
When I perform undo operation, both StartingUndo and FinsihedUndo’s edits contain AddedNode, whereas the operation to be performed is RemovedNode(as it an undo operation).
Is there any way to access the Transaction details during the Undo/Redo operation?
Ah, so the model does not represent the full and true state of the (logical) diagram. That might be a problem.
Yes, sorry, that’s a naming convention: those four events are “Transaction” events, since they don’t represent actual changes to any model data, but are just notifications that an undo or a redo is about to or has just happened. There are also similar events about the actual starting and committing of a transaction.
Yes, you are already looking at the CompoundEdit that was just undone or redone. (The class is a nested class of UndoManager.) That has the complete list of edits of that transaction. What else are you looking for? If you are looking for anything relating to Node or Link or Diagram, sorry – but we can only keep track of model state, not that of any FrameworkElements.