diff --git a/Perspex.Controls/Grid.cs b/Perspex.Controls/Grid.cs index ffb0cc6149..c2f4f9c3d6 100644 --- a/Perspex.Controls/Grid.cs +++ b/Perspex.Controls/Grid.cs @@ -527,10 +527,11 @@ namespace Perspex.Controls private void ExpandStarCols(Size availableSize) { + int matrixCount = this.colMatrix.GetLength(0); int columnsCount = this.ColumnDefinitions.Count; double width = availableSize.Width; - for (int i = 0; i < this.colMatrix.GetLength(0); i++) + for (int i = 0; i < matrixCount; i++) { if (this.colMatrix[i, i].Type == GridUnitType.Star) { @@ -542,12 +543,12 @@ namespace Perspex.Controls } } - this.AssignSize(this.colMatrix, 0, this.colMatrix.GetLength(0) - 1, ref width, GridUnitType.Star, false); + this.AssignSize(this.colMatrix, 0, matrixCount - 1, ref width, GridUnitType.Star, false); width = Math.Max(0, width); if (columnsCount > 0) { - for (int i = 0; i < this.colMatrix.GetLength(0); i++) + for (int i = 0; i < matrixCount; i++) { if (this.colMatrix[i, i].Type == GridUnitType.Star) { @@ -559,13 +560,14 @@ namespace Perspex.Controls private void ExpandStarRows(Size availableSize) { + int matrixCount = this.rowMatrix.GetLength(0); int rowCount = this.RowDefinitions.Count; double height = availableSize.Height; // When expanding star rows, we need to zero out their height before // calling AssignSize. AssignSize takes care of distributing the // available size when there are Mins and Maxs applied. - for (int i = 0; i < this.rowMatrix.GetLength(0); i++) + for (int i = 0; i < matrixCount; i++) { if (this.rowMatrix[i, i].Type == GridUnitType.Star) { @@ -577,11 +579,11 @@ namespace Perspex.Controls } } - this.AssignSize(this.rowMatrix, 0, this.rowMatrix.GetLength(0) - 1, ref height, GridUnitType.Star, false); + this.AssignSize(this.rowMatrix, 0, matrixCount - 1, ref height, GridUnitType.Star, false); if (rowCount > 0) { - for (int i = 0; i < this.rowMatrix.GetLength(0); i++) + for (int i = 0; i < matrixCount; i++) { if (this.rowMatrix[i, i].Type == GridUnitType.Star) { @@ -703,12 +705,15 @@ namespace Perspex.Controls } } - for (int r = 0; r < this.rowMatrix.GetLength(0); r++) + int rowMatrixDim = this.rowMatrix.GetLength(0); + int colMatrixDim = this.colMatrix.GetLength(0); + + for (int r = 0; r < rowMatrixDim; r++) { this.rowMatrix[r, r].OfferedSize = this.rowMatrix[r, r].DesiredSize; } - for (int c = 0; c < this.colMatrix.GetLength(0); c++) + for (int c = 0; c < colMatrixDim; c++) { this.colMatrix[c, c].OfferedSize = this.colMatrix[c, c].DesiredSize; } @@ -716,17 +721,20 @@ namespace Perspex.Controls private void SaveMeasureResults() { - for (int i = 0; i < this.rowMatrix.GetLength(0); i++) + int rowMatrixDim = this.rowMatrix.GetLength(0); + int colMatrixDim = this.colMatrix.GetLength(0); + + for (int i = 0; i < rowMatrixDim; i++) { - for (int j = 0; j < this.rowMatrix.GetLength(0); j++) + for (int j = 0; j < rowMatrixDim; j++) { this.rowMatrix[i, j].OriginalSize = this.rowMatrix[i, j].OfferedSize; } } - for (int i = 0; i < this.colMatrix.GetLength(0); i++) + for (int i = 0; i < colMatrixDim; i++) { - for (int j = 0; j < this.colMatrix.GetLength(0); j++) + for (int j = 0; j < colMatrixDim; j++) { this.colMatrix[i, j].OriginalSize = this.colMatrix[i, j].OfferedSize; } @@ -735,17 +743,20 @@ namespace Perspex.Controls private void RestoreMeasureResults() { - for (int i = 0; i < this.rowMatrix.GetLength(0); i++) + int rowMatrixDim = this.rowMatrix.GetLength(0); + int colMatrixDim = this.colMatrix.GetLength(0); + + for (int i = 0; i < rowMatrixDim; i++) { - for (int j = 0; j < this.rowMatrix.GetLength(0); j++) + for (int j = 0; j < rowMatrixDim; j++) { this.rowMatrix[i, j].OfferedSize = this.rowMatrix[i, j].OriginalSize; } } - for (int i = 0; i < this.colMatrix.GetLength(0); i++) + for (int i = 0; i < colMatrixDim; i++) { - for (int j = 0; j < this.colMatrix.GetLength(0); j++) + for (int j = 0; j < colMatrixDim; j++) { this.colMatrix[i, j].OfferedSize = this.colMatrix[i, j].OriginalSize; } @@ -802,6 +813,9 @@ namespace Perspex.Controls { public GridWalker(Grid grid, Segment[,] rowMatrix, Segment[,] colMatrix) { + int rowMatrixDim = rowMatrix.GetLength(0); + int colMatrixDim = colMatrix.GetLength(0); + foreach (Control child in grid.Children) { bool starCol = false; @@ -809,10 +823,10 @@ namespace Perspex.Controls bool autoCol = false; bool autoRow = false; - int col = Math.Min(Grid.GetColumn(child), colMatrix.GetLength(0) - 1); - int row = Math.Min(Grid.GetRow(child), rowMatrix.GetLength(0) - 1); - int colspan = Math.Min(Grid.GetColumnSpan(child), colMatrix.GetLength(0) - 1); - int rowspan = Math.Min(Grid.GetRowSpan(child), rowMatrix.GetLength(0) - 1); + int col = Math.Min(Grid.GetColumn(child), colMatrixDim - 1); + int row = Math.Min(Grid.GetRow(child), rowMatrixDim - 1); + int colspan = Math.Min(Grid.GetColumnSpan(child), colMatrixDim - 1); + int rowspan = Math.Min(Grid.GetRowSpan(child), rowMatrixDim - 1); for (int r = row; r < row + rowspan; r++) {