/// <summary> /// Raises the Layout event. /// </summary> /// <param name="e">A LayoutEventArgs that contains the event data.</param> protected override void OnLayout(LayoutEventArgs e) { // Call the base class's method so that registered delegates receive the event. base.OnLayout(e); // If we have no parent, no layout is required. if (Parent == null) { return; } // Layout the command bar lightweight controls. The return is the column layout rectangle. Rectangle columnLayoutRectangle = LayoutCommandBars(); // Set the initial (to be adjusted below) column widths. int leftColumnWidth = LeftColumnPreferredWidth; int centerColumnWidth = CenterColumnMinimumWidth; int rightColumnWidth = RightColumnPreferredWidth; // Adjust the column widths as needed for the layout width. if (leftColumnWidth + centerColumnWidth + rightColumnWidth > columnLayoutRectangle.Width) { // Calculate the width that is available to the left and right columns. int availableWidth = columnLayoutRectangle.Width - centerColumnWidth; // Adjust the left and right column widths. if (LeftColumnVisible && RightColumnVisible) { // Calculate the relative width of the left column. double leftColumnRelativeWidth = ((double)leftColumnWidth) / (leftColumnWidth + rightColumnWidth); // Adjust the left and right column widths. leftColumnWidth = Math.Max((int)(leftColumnRelativeWidth * availableWidth), LeftColumnMinimumWidth); rightColumnWidth = Math.Max(availableWidth - leftColumnWidth, RightColumnMinimumWidth); } else if (LeftColumnVisible) { // Only the left column is visible, so it gets all the available width. leftColumnWidth = Math.Max(availableWidth, LeftColumnMinimumWidth); } else if (RightColumnVisible) { // Only the right column is visible, so it gets all the available width. rightColumnWidth = Math.Max(availableWidth, RightColumnMinimumWidth); } } else { // We have a surplus of room. Allocate additional space to the center column, if // if is visible, or the left column if it is not. if (CenterColumnVisible) { centerColumnWidth = columnLayoutRectangle.Width - (leftColumnWidth + rightColumnWidth); } else { leftColumnWidth = columnLayoutRectangle.Width - rightColumnWidth; } } // Set the layout X offset. int layoutX = columnLayoutRectangle.X; // Layout the left column, if it is visible. if (LeftColumnVisible) { // Set the virtual bounds of the left column. leftColumn.VirtualBounds = new Rectangle(layoutX, columnLayoutRectangle.Y, leftColumnWidth, columnLayoutRectangle.Height); leftColumn.PerformLayout(); // Adjust the layout X to account for the left column. layoutX += leftColumnWidth; // Update the left column vertical splitter and maximum column width. if (CenterColumnVisible) { // Turn on the left column vertical splitter on the right side. leftColumn.VerticalSplitterStyle = VerticalSplitterStyle.Right; // Set the left column's maximum width. leftColumn.MaximumColumnWidth = columnLayoutRectangle.Width - (CenterColumnMinimumWidth + this.RightColumnPreferredWidth); } else { leftColumn.VerticalSplitterStyle = VerticalSplitterStyle.None; leftColumn.MaximumColumnWidth = 0; } } // Layout the center column. if (CenterColumnVisible) { // Set the virtual bounds of the center column. centerColumn.VirtualBounds = new Rectangle(layoutX, columnLayoutRectangle.Y, centerColumnWidth, columnLayoutRectangle.Height); centerColumn.PerformLayout(); // Adjust the layout X to account for the center column. layoutX += centerColumnWidth; // The center column never has a vertical splitter or a maximum column width. centerColumn.VerticalSplitterStyle = VerticalSplitterStyle.None; centerColumn.MaximumColumnWidth = 0; } // Layout the right column. if (RightColumnVisible) { // Set the virtual bounds of the right column. rightColumn.VirtualBounds = new Rectangle(layoutX, columnLayoutRectangle.Y, rightColumnWidth, columnLayoutRectangle.Height); rightColumn.PerformLayout(); // Update the right column vertical splitter and maximum column width. if (CenterColumnVisible || LeftColumnVisible) { // Turn on the right column's vertical splitter on the left side. rightColumn.VerticalSplitterStyle = VerticalSplitterStyle.Left; // Set the right column's maximum width. rightColumn.MaximumColumnWidth = columnLayoutRectangle.Width - (LeftColumnPreferredWidth + CenterColumnMinimumWidth); } else { rightColumn.VerticalSplitterStyle = VerticalSplitterStyle.None; rightColumn.MaximumColumnWidth = 0; } } }