When is the SelectionCopied fired?

Hi everyone,

Am implementing a Copy/Paste in a GraphLinksModel. I need to do some processing on the newly pasted elements. And for that I need info on the elements they were copied from.

I hooked a command handler on the diagram’s SelectionCopied event but it doesn’t hit during the copy and paste. I expected it to hit when pressing CTRL+C but that’s not the case.
I tried the same scenario on the “Dynamic Ports” sample and it’s the same result, my handler is never hit.

So when is that event raised?

There is a SelectionChanged event, but I find relying on that one very heavy and aggressive.

Thanks

You want the “ClipboardPasted” DiagramEvent. “SelectionCopied” is for copying by the DraggingTool.

Oops, wrong product/platform. But still basically the right answer.

You want the Diagram.ClipboardPasted event. Diagram.SelectionCopied is for copying by the DraggingTool.

http://goxam.com/2.2/helpWPF/webframe.html#Northwoods.GoWPF~Northwoods.GoXam.Diagram~ClipboardPasted_EV.html

Hi Walter,

Thank you for your answer. But how do I get the original elements (the ones used to copy from)?
In the DiagramEventArgs I receive, both “Element” and “Part” properties are null.

The clipboard does not contain any Parts (i.e. FrameworkElements) but just data (IDataCollection).

So I’m not sure what you are looking for, because each selected Node’s Data should have the information that you need.

Remember also that the original Parts that were copied into the clipboard might already be deleted, or might have come from a different application.

Am actually copying from the same application. After a “Paste”, the selected node is the new copy.
Here is my use case:
I have a subgraph containing many nodes. I select a node and then Copy&Paste it. The newly copied node has some inconsistent properties, and one of these is the SubGraphKey (the new node can float anywhere and that’s not good in my case).
That’s why I need the original node’s SubGraphKey value.

So, other then registering to SelectionChanged event, is there a way to get the original node’s data during the Copy&Paste process?

So you need to make sure the copied data (i.e. the new data objects that were created to be in the clipboard, in case the original nodes were deleted or modified) includes the key of the containing Group.

I haven’t tried this, but I think you can copy that information in an override of GraphLinksModel.CopyNode2. In your case the newgroup argument to that method will be null, because the Group won’t have been copied. But you can still look at the original node data and copy over any containing Group key.

protected override void CopyNode2(NodeType oldnodedata, CopyDictionary env, NodeType newnodedata, NodeType newgroup, IEnumerable<NodeType> newmembers) {
    base.CopyNode2(oldnodedata, env, newnodedata, newgroup, newmembers);
    newnodedata.SubGraphKey = oldnodedata.SubGraphKey;  // OR EQUIVALENT, depending on your NodeType data class
}

Hi again,

Thank you for the answer.
But I noticed that the CopyNode2 method is hit twice: on CTRL+C and on CTRL+V.
So for a node DummyNode we have:
On CTRL+C: oldnodedata -> DummyNode, newnodedata -> DummyNode_Copy
On CTRL+V: oldnodedata -> DummyNode_Copy, newnodedata -> DummyNode_NewCopy

The node is actually copied twice. Why is that? Because now the processing I’ll put in this method will be done twice.

Thanks

Well, there are two commands: Commands.Copy, implemented by CommandHandler.Copy(), and Commands.Paste, implemented by CommandHandler.Paste().

Each command has to copy some data. Copy calls CommandHandler.CopyToClipboard; Paste calls CommandHandler.PasteFromClipboard.

As I said above, the clipboard has to hold its own copy of the model data, because the clipboard must remain available and stable even if the original Parts are deleted or modified.

And of course each paste has to perform its own copy, since it’s easy to paste repeatedly, expecting multiple copies of the same data.

All of this follows standard conventions regarding the clipboard and copy-and-paste, which window systems (and Windows in particular) established decades ago.