Adding a link with a specific (custom) template:

I have the need to create a link programatically, and am able to do so using the AddLink method (code below). Is there a way to force the link to be created from a specific link template (other than the default)?

my current workin code snippet:
MyNodeData to = new MyNodeData();
MyNodeData from = new MyNodeData();
to.Key = "Customer";
from.Key = "Cloud";
myDiagram.StartTransaction("Add Link");
myDiagram.Model.AddLink(from, null, to, null);
myDiagram.CommitTransaction("Add Link");

You can use as many DataTemplates as you want. Just put them into a DataTemplateDictionary and assign Diagram.LinkTemplateDictionary. The keys are strings that name the templates.

Then you set the data.Category for each link data to be the name of the template you want to represent the link.

This same technique also works with node data. There’s a section in GoXamIntro.pdf that describes this. And there are several samples that make use of it. You can also change the template dynamically just by modifying the data.Category property.

Perfect - thanks for the direction

I am very close Walter - I can feel it. I have been using a data dictionary successfully for nodes, and per your direction am now doing the same now for links. However, I am not able to get the model to set the “category” attribute from code. I can only get it to work if I hard code it in the MyLinkData class (example below).

not working:
private void Link_Click(object sender, RoutedEventArgs e)
{
myDiagram.StartTransaction("Add Link");
MyNodeData to = new MyNodeData();
MyNodeData from = new MyNodeData();
MyLinkData ComLink = new MyLinkData();
to.Key = "Customer";
from.Key = "Cloud";
ComLink.Category = "LinkTemplateCom";
myDiagram.Model.AddLink(from, null, to, null);
myDiagram.CommitTransaction("Add Link");
}
working if I hard code it in the class:
public class MyLinkData : GraphLinksModelLinkData<String, String>
{
public MyLinkData()
{
this.Category = "LinkTemplateCom";
}

You are allocating a MyLinkData in your Click event handler, setting its Category property, and then throwing that link data away.

Also you are constructing two MyNodeDatas and then throwing them away.

I’m not sure what you really want to do, but normally when you want to add a link programmatically you first find the existing node data(s) that you want to link up, and then you call Model.AddLink.

If you want to programmatically change the Category or any other properties, you can do so on the link data object that is returned by AddLink.

I thought that the AddLink method consumed the properties that I had added to the new MyLinkData object. My goal is to be able to programatically create a link between two existing nodes, but also specify a non-default link from the DataTemplateDictionary holding the link templates. The code in the event hadler above does create a link between the two nodes being specified, but does use the “LinkTemplateCom” as I was hoping.