How are links created

Hey there,

Can anybody tell me how I can decide if a link was created by the user, or by code.

When I write the data back to my database ( in the OnChange override like the manual says) it works fine if the Link was created by the user interface.

But this method gets also called when i add a new link in code ( when an other component changes the data and I have to update the links).

So I would try to update the data again ! ! !

Here the code : The Connect Method commits the data to the database !

      protected override void OnChanged(ModelChangedEventArgs e)
         if (e.Change == ModelChange.AddedLink)
            var wire = e.Data as Wire;
            var sourceNode = FindNodeByKey(wire.From);
            var sinkNode = FindNodeByKey(wire.To);
            var createdConnections = Connect(sourceNode, wire.FromPort, sinkNode, wire.ToPort);
            wire.Connections = createdConnections;

Is there a build in way to solve this problem ?
Can I find out if the Link (Wire in my case) was created from the user interface or in code ?
Or is this the wron way to persist the data ?

Thanks in advance


Normally if you want to do something only when the user’s gesture or command invokes GoXam code that performs some change, you would implement a Diagram event handler for that. In this case that would be the Diagram.LinkDrawn event.

You might also be interested in the Diagram.SelectionDeleting and LinkRelinked events.

But even if you continue to implement a DiagramModel.Changed event handler (which is what overriding DiagramModel.OnChanged does) you can still distinguish the causes by looking at the Diagram.CurrentTool or other diagram state if you know which Diagram might have caused the model change You can see if it is the kind of tool that you care about, such as LinkingTool, or you can look at the state of the tool for additional information, such as RelinkingTool.OriginalToNode.

Note however that looking at the current Diagram in a model method such as the OnChanged override is considered bad design, because it assumes knowledge of which of potentially several Diagrams is the actual source of the Model change. It would be better to implement a Changed event handler in code associated with the particular Diagram.

Hey walter, thanks for the quick reply

The solution with the diagram events is of course the better way.
But I thought about a way of not needing to attach sereral events, when assembling the view and the viewmodel. (Only to forward them to the viewmodel)

Because it is neccesary do inherit the given viewmodel classes, I thougth this base class could provide some virtual method e.g. OnLinkCreated… that I can use !

As there is no such way, I will take the other one Disapprove

Thanks Daniel