If I add nodes by calling mode.Add(node) repeatly, the speed is very slow. I guess this is caused by event handlers that responds to node adding events. Could I add a list of nodes without envoke those event handlers? Or is there any way that I could add a lot of nodes quickly?
The PartManager class listens for the model’s Changed events. It will create new Nodes corresponding to the new model data. So it needs to get ModelChange events if you want the diagram to display the new nodes.
If you don’t mind it rebuilding all of the nodes, I suppose you could remove the model from the Diagram by setting Diagram.Model to null, add all of the node data by calling model.AddNode repeatedly, and then reset Diagram.Model. Although that will avoid some of the incremental update overhead, I believe that because most of the time goes to actually applying (“expanding”) the templates, it probably won’t save you any time.
Ah, that’s a question for Microsoft.
But since you probably won’t get a satisfactory answer from them any time soon, I suggest you keep your templates as simple as you can. Try to avoid including Controls. Try to limit data-binding.
Another way to address the general problem is to do more in the way of incremental exposition.
Both of the samples IncrementalTree and Grouping demonstrate this technique where the nodes are not created until the user clicks to “expand”. In fact they demonstrate not just creating the Nodes but also creating the node data on demand.
I don’t know if such approaches are appropriate to your application, but it might be worth considering.
Oh, and if you think using a different thread would be a solution, note that all of the elements need to be created and modified on the “main” UI thread.