/// <summary> Put as many controls from the collection on the current page. </summary> /// <param name="currentPage"> The current page on which controls should be laid out. </param> /// <param name="controlIndex"> The index of the first control to add. </param> /// <returns> The index of the next control to lay out on the next page. </returns> private int LayoutAsManyControlsAsPossible(SinglePrintedPage currentPage, int controlIndex) { bool hasMoreSpace = true; while (controlIndex < _controlsToPlace.Count && hasMoreSpace) { var controlToPlace = _controlsToPlace[controlIndex]; currentPage.PrimaryContent.Children.Add(controlToPlace); currentPage.UpdateLayout(); currentPage.Measure(new Size(currentPage.Width, currentPage.Height)); currentPage.Arrange(new Rect(0, 0, currentPage.Width, currentPage.Height)); if (!IsUserVisible(controlToPlace, currentPage.AllowedSpace)) { hasMoreSpace = false; currentPage.PrimaryContent.Children.Remove(controlToPlace); controlIndex--; if (controlIndex > 0) { // titles are always attached to the next sub-section var previousControl = _controlsToPlace[controlIndex]; if ((previousControl.Content as SubSectionEntryViewModel)?.SectionType == SubSectionType.Title) { currentPage.PrimaryContent.Children.Remove(previousControl); controlIndex--; } } } controlIndex++; } return(controlIndex); }