Re-layout when diagram size changes?


I have a diagram using ForceDirectedLayout. I set Stretch=Uniform in order for the nodes to fill the diagram without scrollbars. How can I make the diagram redo the layout when its size changes?



Are you sure you want to perform a layout each time the viewport changes size?

Well, if you really do, you need to implement a DiagramPanel.ViewportBoundsChanged event handler that checks to see that the panel size has actually changed, and then request a layout. This is how:

myDiagram.TemplateApplied += (sender, evt) => { myDiagram.Panel.ViewportBoundsChanged += (s, e) => { if (e.OldValue.Width != e.NewValue.Width || e.OldValue.Height != e.NewValue.Height) { myDiagram.LayoutDiagram(); } }; };
Note that the event handler is set up within a Diagram.TemplateApplied event handler, to make sure that the Diagram.Panel exists first.