[Solved] Problem with linking nodes

Hi,

I'm trying to add links automatically when a node is added to the diagram. The linking is done based on the drop location relative to the selected node in the diagram.

The issue is the linking is done successfully. But, for some reason after the linking has been done the link is removed and also the new node that was added. And the node is added again to the diagram. Below is the activities that happens when a new node is added (Used Debug.WriteLine to print out the Change type and the source node key):

[Note: “New Key” is the key of the data that is passed in the event argument(e.Data)]

AddedNode
New Key: 3
StartedTransaction
AddedFromNodeKey
New Key: 2
AddedToNodeKey
New Key: 3
CommittedTransaction
RemovedToNodeKey
New Key: 3
StartedTransaction
CommittedTransaction
StartedTransaction
CommittedTransaction
RemovingNode
New Key: 3
RemovedNode
New Key: 3
StartedTransaction
AddedNode
New Key: 3
AddedToNodeKey
New Key: 3
RemovedToNodeKey
New Key: 3
CommittedTransaction
StartedTransaction
CommittedTransaction
StartedTransaction
CommittedTransaction

Below is the code snippet of the function handling Diagram’s Model Changed event

if(e.Change == ModelChange.AddedNode)
{
     if(diagram.SelectedNode != null)
     {
          e.Model.StartTransaction("AutoLink");
          if( (e.Data as NodeData).Location.X > diagram.SelectedNode.Location.X)
               diagram.Model.AddLink(diagram.SelectedNode.Data, null, e.Data, null);
          else
               diagram.Model.AddLink(e.Data, null, diagram.SelectedNode.Data, null);
          e.Model.CommitTransaction("AutoLink");
     }
}

I’m not able to understand why the node is added & then removed & added again. Why the link is removed?

I’ve defined the incoming & outgoing port using go:Node.ToSpot = “MiddleLeft” & go:Node.FromSpot = “MiddleRight” on the go:NodePanel in the node’s DataTemplate.

I’m using Silverlight 4.0.60129.0, .Net 4 framework, goSilverlight 1.2.6.4

Thank you.

Is this a drop from another control such as a Palette?

If so, the diagram adds a temporary node that the user drags around. Upon a drop or leave or cancel that temporary node is removed. Finally as part of only a drop the real node data is added to the model and a Diagram.ExternalObjectsDropped event is raised.

So I believe the problem is that you are reacting to the temporary node being added, when you really should be ignoring it. It’s only when the permanent node is added upon a successful drop that you should add the link.

The normal way of doing that is to implement a Diagram.ExternalObjectsDropped event handler.

Yes, it is a drop from the Palette.

The code snippet mentioned earlier worked fine in WPF. I wonder why it didn’t work for Silverlight. Anyways, I changed the code in Silverlight version as per your suggestion and it works.

Thanks for the help Clap

You’re welcome.

That should work in WPF too, if you want to keep the code the same for both platforms.

I can’t explain why your original code worked in WPF. Perhaps there was some other difference to account for that.

More generally, if you really wanted to use Model.Changed events, would be to look to see if Node.Layer.IsTemporary is true – if so, ignore it.

However, I think trying to do that stuff at the model level is inappropriate, since it really depends on transient state such as the selection. That’s why you really want to do this kind of behavior at the diagram level, not the model level.

Oh this one is very good. Very useful for my new requirement.

Thanks a lot