Silverlight Error “Layout Cycle Detected Layout could not complete”


Recently our Managing Director reported that while presenting the system froze with a white screen, or sometimes just the screen went un-responsive without no apparent error message on the screen.
Digging a bit deeper into it we found that there was a

“Layout Cycle Detected Layout could not complete”

that was being thrown.
Basically, in the page’ SizeChanged event, there was some code that was modifying the width of one of the panels on the page to accomodate to fit the content of the data field in the controls in the panel (which should ideally be done by setting width/height properties of the container elements to “Auto”), so causing the properties of the components to change while the layout was still being updated, ultimately resulting in this exception.

Once the containers were re-designed to Auto Size for the content (Also imposing a max width and max height with a Horizontal scrollbar to display content sensibly), the problem disappeared and away we went 🙂