Conditional templates for nodes?

Hello,

can I somehow configure conditional data templates for nodes depending on some bound property ? I would like to add a type attribute that should switch between different representations of a node.

Any ideas would be helpful.

Lars

Sure, that’s what DataTemplateDictionary is for.

There are three Diagram properties that are of type DataTemplateDictionary: NodeTemplateDictionary, LinkTemplateDictionary, and GroupTemplateDictionary.

Each dictionary maps category names (strings) to DataTemplates. So the “type attribute” that you are talking about is what we call a “category” of node or link or group.

So if you add a string property on your node data class, you can tell the model that that property supplies the category for each node by setting the …Model.NodeCategoryPath property to that property name.

If you are using our predefined …ModelNodeData classes, you don’t need to define such a property at all, because they already have a Category property. And each …Model generic class automatically sets its own NodeCategoryPath property to “Category” if you are using one of the predefined data classes.

And to be complete I should say that the Diagram.NodeTemplate property corresponds to the default entry in the NodeTemplateDictionary with the category name of “”, the empty string. But if you set both the Diagram.NodeTemplate property and the Diagram.NodeTemplateDictionary property, the NodeTemplate takes precedence over the NodeTemplateDictionary.Default value. So one normally sets one or the other property, but not both.

Also, in version 1.0, the models assume the Category does not change. But we may remove that restriction in the future.

Several of the samples make use of DataTemplateDictionaries.

There’s a discussion of DataTemplateDictionaries in GoXamIntro.pdf. It has an example of how you might swap template dictionaries as the DiagramPanel.Scale changes.


Thanks a lot, Walter, your quick help is appreciated.

I used the dictionary and worked out what I wanted in a few minutes, great!

For my desired prototype, I do not need different templates bound to a property, that’s fine.



There is one open issue regarding the dictionaries:

- In the examples, the dictionary is defined in XAML as an empty one and filled by code. This is what I did as well.

- In the documentation, the templates are shown as nested elements of the dictionary.



What are the advantages/disadvantages of either approach ?



Lars

Silverlight doesn’t support defining dictionary entries as nested elements in XAML. WPF does.

Because we wanted to have all of the C# code files be identical for both platforms, the samples use the most common functionality.