How to add a collection of Nodes?

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?

Thanks!

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.

Yes, I test both approaches and find it only cause 3% performance difference.
So is there anyway to make applying the templates more quickly?
Thanks!

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.

Thank you walter. I modified the sample "DynamicPorts" to add 1000 nodes when the right button up and the mouse pointer is on an empty place. It cost about 5-6 seconds to display these nodes on the panel. The template is very simple (just a rectangle and a text box). It's really slow in my application scenario.
Thanks. But incremental exposition is not suitable for me. I have to add a lot of nodes immediately after the diagram is displayed.