Wrong template used if NodeSource with items is set on constructor only

Hi Walter,

I have a strange phenomenon when my source for nodes already contains elements and it is allied to the diagram’s NodeSource before Diagram is loaded.

In this case all the groups that are pre-created have the node type.
If on the other hand I define the source at the “loaded” event, the groups created have the group type.

Do I make an error when initializing the diagram.

Here are 2 examples :
NodeSource is set on constructor

Diagram definition
<gowpf:Diagram x:Name="myDiagramNodeAndLinks"
                   Grid.Row="1"
                   InitialStretch="Unstretched"
                   HorizontalContentAlignment="Stretch"
                   VerticalContentAlignment="Stretch"
                   NodeTemplate="{StaticResource GraphNodeTemplate}"
                   LinkTemplate="{StaticResource GraphLinkTemplate}"
                   GroupTemplate="{StaticResource GraphGroupTemplate}"
                   MaximumSelectionCount="1"                   
                   InitialLayoutCompleted="myDiagramNodeAndLinks_Initialized"
                   DragSelectingTool="{x:Null}"
                   MouseLeftButtonUp="myDiagramNodeAndLinks_MouseLeftButtonUp"
                   PreviewKeyDown="myDiagramNodeAndLinks_KeyDown"
                   AllowMove="False">

image

NodeSource is set on Loaded event

Diagram definition
<gowpf:Diagram x:Name="myDiagramNodeAndLinks"
                   Grid.Row="1"
                   InitialStretch="Unstretched"
                   HorizontalContentAlignment="Stretch"
                   VerticalContentAlignment="Stretch"
                   NodeTemplate="{StaticResource GraphNodeTemplate}"
                   LinkTemplate="{StaticResource GraphLinkTemplate}"
                   GroupTemplate="{StaticResource GraphGroupTemplate}"
                   MaximumSelectionCount="1"                   
                   InitialLayoutCompleted="myDiagramNodeAndLinks_Initialized"
                   DragSelectingTool="{x:Null}"
                   MouseLeftButtonUp="myDiagramNodeAndLinks_MouseLeftButtonUp"
                   PreviewKeyDown="myDiagramNodeAndLinks_KeyDown"
                   AllowMove="False"
                   Loaded="myDiagramNodeAndLinks_Loaded">

Sincerely

JJ

How do you set up the model?

I create nodes manually with the three following commands

Model.DoNodeAdded(groupData);
Model.DoGroupNodeChanged(groupData);
group = AddNodeForData(groupData);

We use virtualization for performance.
For convenience all root node are always present in the diagram.

Does your NodesSource collection implement INotifyCollectionChanged? If yes, then you should not be calling DoNodeAdded, which is called automatically if the collection implements that interface.

Does the node data class implement INotifyPropertyChanged? If yes, then you should not be calling DoGroupNodeChanged, which is called automatically if the data object implements that interface.

Programmatically adding a node should be done by calling AddNode on the model. Don’t call PartManager.AddNodeForData.

No Node list is a simple List. Adding a group in list is not a reason the add it in the diagram.
For performance we define which items are present or not on the diagram, and it works fine.

My current problem is if node source contains already element when diagram is loaded, template used is node’s template, not group’s template. In this case I do not set the model, its all done internally by your dll, certainly because for all item which are root element. As said above root groups are always.present on the diagram.

Ah, yes if you are virtualizing the diagram, that is distinctly more programming work to do, since you need to customize the PartManager to do what you want.

You say “if node source contains already element when diagram is loaded”. Are you saying that when you assign the Diagram.Model with a model that already has a bunch of node data, including data that are groups, the PartManager chooses the wrong template?

If so, that is probably because at the time that DoNodeAdded is called, FindIsGroupForNode is returning false for that data object. If you have overridden GraphLinksModel.FindIsGroupForNode, check that implementation. If you haven’t, check the value of GraphLinksModel.NodeIsGroupPath.

And check your data of course, to make sure that its data property is set the way that you want.

You say “if node source contains already element when diagram is loaded”. Are you saying that when you assign the Diagram.Model with a model that already has a bunch of node data, including data that are groups, the PartManager chooses the wrong template?
Yes.

Group Data are by default set (in ctor) with attribute “IsSubGraph = true;”
Here the status of data when added to the linked node source (Data collected in method FilterNodeForData)


As you can see everything look fine.

Does the incorrectly chosen template at least have the correct template name?

You haven’t overridden PartManager.FindTemplateForNode, have you?

No I haven’t overridden it.

If you tell me how I can check it, I will check it.

I’ve just test without specified template (for Node or Group), first visual node created are still Node other are groups.

It’s look like that FindTemplateForNode return the right template:


Here with value extracted from nodeData

Template is GroupTemplate

FYI:
A precision. In my unsuccessful case, UserControl containing the diagram is not loaded on program start, as it is place in a non selected desktop when main program is started/running.
That the reason why node source contains a set of data when Diagram is loaded.

So your UserControl’s Diagram is initialized with model data, and in that case the chosen templates are wrong for instantiating the nodes?

I still don’t understand what’s going on, but maybe you shouldn’t be constructing the diagram with model data, and only assign it later.

As told before, setting Node source when diagram is loaded solved my problem. I think that my problem has something to do with initialization process, may be from XAML.
If you find something on your side, please let me know it.

Regard.

JJ