/// <summary> /// Recomputes rows and update children. /// </summary> private void InvalidateGridParameters() { RowDefinitionCollection rowCollection = RowDefinitions; UIElementCollection children = Children; // Determine how many rows we need int remainder; int neededRowCount = Math.DivRem(children.Count, 2, out remainder) + remainder; int currentRowCount = rowCollection.Count; int deltaRowCount = neededRowCount - currentRowCount; // Add/remove rows if (deltaRowCount > 0) { for (int i = 0; i < deltaRowCount; i++) { rowCollection.Add(new RowDefinition { Height = new GridLength(0.0, GridUnitType.Auto) }); } } else if (deltaRowCount < 0) { rowCollection.RemoveRange(currentRowCount + deltaRowCount, -deltaRowCount); } // Update Grid.Row and Grid.Column dependency properties on each child control int row = 0; int column = 0; foreach (UIElement element in children) { element.SetValue(ColumnProperty, column); element.SetValue(RowProperty, row); if (column == 0 && GetUseFullRow(element)) { element.SetValue(ColumnSpanProperty, 2); row++; } else { column++; if (column > 1) { column = 0; row++; } } } }
private void InvalidateGridParameters() { RowDefinitionCollection rowCollection = RowDefinitions; UIElementCollection children = Children; int remainder; int neededRowCount = Math.DivRem(children.Count, 2, out remainder); if (remainder > 0) { neededRowCount++; } int currentRowCount = rowCollection.Count; int deltaRowCount = neededRowCount - currentRowCount; if (deltaRowCount > 0) { for (int i = 0; i < deltaRowCount; i++) { rowCollection.Add(new RowDefinition { Height = new GridLength(0.0, GridUnitType.Auto) }); } } else if (deltaRowCount < 0) { rowCollection.RemoveRange(currentRowCount + deltaRowCount, -deltaRowCount); } int row = 0; int column = 0; foreach (UIElement element in children) { element.SetValue(Grid.ColumnProperty, column); element.SetValue(Grid.RowProperty, row); column++; if (column > 1) { column = 0; row++; } } }