private void CalcColumnWidthes(MeasureData[] data, double widthConstrait) { var columnDefinitions = PanelContainer.ColumnDefinitions.ToArray(); if (data.Length <= 0) { return; } var columnData = new ColumnData[data[data.Length - 1].ColumnIndex + 1]; for (int i = 0; i < columnData.Length; i++) { columnData[i] = i < columnDefinitions.Length ? new ColumnData(columnDefinitions[i]) : new ColumnData(); } foreach (MeasureData child in data .Where(child => child.ColumnIndex >= columnDefinitions.Length)) { columnData[child.ColumnIndex].Minimum = Math.Max(child.DesiredSize.Width, columnData[child.ColumnIndex].Minimum); } bool stretchAuto = !columnData.Any(column => column.Value.IsStar) && PanelContainer.HorizontalContentAlignment == HorizontalAlignment.Stretch; foreach (ColumnData column in columnData) { if (column.Value.IsAuto && stretchAuto) { column.Value = new GridLength(1, GridUnitType.Star); } else if (!column.IsStar) { column.ActualValue = column.IsAuto ? column.Minimum : Math.Min(column.Maximum, Math.Max(column.Minimum, column.Value.Value)); column.Final = true; } } DistributeStarWidth(columnData, widthConstrait); double extraSpace = Math.Max(0, widthConstrait - columnData.Sum(column => column.ActualValue)); double accumulatedWidth = 0; foreach (ColumnData column in columnData) { column.Offset = accumulatedWidth; switch (PanelContainer.HorizontalContentAlignment) { case HorizontalAlignment.Center: column.Offset += extraSpace / 2; break; case HorizontalAlignment.Right: column.Offset += extraSpace; break; } accumulatedWidth += column.ActualValue; } for (var i = 0; i < data.Length; i++) { var rect = data[i].Rect; rect.X = columnData[data[i].ColumnIndex].Offset; rect.Width = data[i].DesiredSize.Width; switch (data[i].HorizontalAlignment) { case HorizontalAlignment.Center: rect.X += (columnData[data[i].ColumnIndex].ActualValue - rect.Width) / 2; break; case HorizontalAlignment.Right: rect.X += columnData[data[i].ColumnIndex].ActualValue - rect.Width; break; case HorizontalAlignment.Stretch: rect.Width = columnData[data[i].ColumnIndex].ActualValue; break; } data[i].Rect = rect; } }