protected override Size ArrangeOverride(Size finalSize) { IDefinitionBase[] currentRowDefinitions = RowDefinitions.Count == 0 ? defaultRowDefinitions : RowDefinitions.ToArray(); IDefinitionBase[] currentColumnDefinitions = ColumnDefinitions.Count == 0 ? defaultColumnDefinitions : ColumnDefinitions.ToArray(); if (currentRowDefinitions.Length == 1 && currentColumnDefinitions.Length == 1) { // optimization return(ArrangeSingleCell(finalSize, currentColumnDefinitions[0], currentRowDefinitions[0])); } double[] rowsLength = currentRowDefinitions.Select(definitionBase => definitionBase.Length.IsAbsolute ? definitionBase.Length.Value : 0).ToArray(); double[] columnsLength = currentColumnDefinitions.Select(definitionBase => definitionBase.Length.IsAbsolute ? definitionBase.Length.Value : 0).ToArray(); int row; int column; int rowSpan; int columnSpan; foreach (FrameworkElement child in Children) { GetChildPosition(child, currentRowDefinitions.Length, currentColumnDefinitions.Length, out row, out column, out rowSpan, out columnSpan); if (rowSpan == 1 && currentRowDefinitions[row].Length.IsAuto) { rowsLength[row] = Math.Max(rowsLength[row], child.DesiredSize.Height); } if (columnSpan == 1 && currentColumnDefinitions[column].Length.IsAuto) { columnsLength[column] = Math.Max(columnsLength[column], child.DesiredSize.Width); } } double rowStarLength = GetStarLength(currentRowDefinitions, finalSize.Height - rowsLength.Sum()); double columnStarLength = GetStarLength(currentColumnDefinitions, finalSize.Width - columnsLength.Sum()); SetStarLengths(currentRowDefinitions, rowStarLength, ref rowsLength); SetStarLengths(currentColumnDefinitions, columnStarLength, ref columnsLength); SetBoundedValues(currentRowDefinitions, ref rowsLength); SetBoundedValues(currentColumnDefinitions, ref columnsLength); SetActualLength(currentRowDefinitions, rowsLength); SetActualLength(currentColumnDefinitions, columnsLength); foreach (FrameworkElement child in Children) { GetChildPosition(child, currentRowDefinitions.Length, currentColumnDefinitions.Length, out row, out column, out rowSpan, out columnSpan); child.Arrange(new Rect( columnsLength.Take(column).Sum(), rowsLength.Take(row).Sum(), columnsLength.Skip(column).Take(columnSpan).Sum(), rowsLength.Skip(row).Take(rowSpan).Sum())); } return(finalSize); }
protected override Size MeasureOverride(Size availableSize) { IDefinitionBase[] currentRowDefinitions = RowDefinitions.Count == 0 ? defaultRowDefinitions : RowDefinitions.ToArray(); IDefinitionBase[] currentColumnDefinitions = ColumnDefinitions.Count == 0 ? defaultColumnDefinitions : ColumnDefinitions.ToArray(); if (currentRowDefinitions.Length == 1 && currentColumnDefinitions.Length == 1) { // optimization return(MeasureSingleCell(availableSize, currentColumnDefinitions[0].Length, currentRowDefinitions[0].Length)); } double[] rowsLength = currentRowDefinitions.Select(definitionBase => definitionBase.Length.IsAbsolute ? definitionBase.Length.Value : 0).ToArray(); double[] columnsLength = currentColumnDefinitions.Select(definitionBase => definitionBase.Length.IsAbsolute ? definitionBase.Length.Value : 0).ToArray(); int row; int column; int rowSpan; int columnSpan; foreach (FrameworkElement child in Children) { GetChildPosition(child, currentRowDefinitions.Length, currentColumnDefinitions.Length, out row, out column, out rowSpan, out columnSpan); child.Measure(new Size( GetMeasureLength(currentColumnDefinitions, availableSize.Width, column, columnSpan), GetMeasureLength(currentRowDefinitions, availableSize.Height, row, rowSpan))); if (rowSpan == 1 && (currentRowDefinitions[row].Length.IsAuto || currentRowDefinitions[row].Length.IsStar)) { rowsLength[row] = Math.Max(rowsLength[row], child.DesiredSize.Height); } if (columnSpan == 1 && (currentColumnDefinitions[column].Length.IsAuto || currentColumnDefinitions[column].Length.IsStar)) { columnsLength[column] = Math.Max(columnsLength[column], child.DesiredSize.Width); } } SetBoundedValues(currentRowDefinitions, ref rowsLength); SetBoundedValues(currentColumnDefinitions, ref columnsLength); return(new Size(columnsLength.Sum(), rowsLength.Sum())); }
public void GridShaping() { //罫線の太さはここで指定。 var thickness = new GridLength(1); // 罫線用の行・列を追加 var columns = ColumnDefinitions.ToArray(); ColumnDefinitions.Clear(); if (columns.Any()) { foreach (var c in columns) { ColumnDefinitions.Add(new ColumnDefinition { Width = thickness }); ColumnDefinitions.Add(c); } } else { ColumnDefinitions.Add(new ColumnDefinition { Width = thickness }); ColumnDefinitions.Add(new ColumnDefinition()); } ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(0.5) }); var rows = RowDefinitions.ToArray(); RowDefinitions.Clear(); if (rows.Any()) { foreach (var r in rows) { RowDefinitions.Add(new RowDefinition { Height = thickness }); RowDefinitions.Add(r); } } else { RowDefinitions.Add(new RowDefinition { Height = thickness }); RowDefinitions.Add(new RowDefinition()); } RowDefinitions.Add(new RowDefinition { Height = thickness }); //行・列を追加した分、Column,Row,ColumnSpan,RowSpanがずれるのでその補正再設定 foreach (UIElement c in Children) { SetColumn(c, GetColumn(c) * 2 + 1); SetColumnSpan(c, GetColumnSpan(c) * 2 - 1); SetRow(c, GetRow(c) * 2 + 1); SetRowSpan(c, GetRowSpan(c) * 2 - 1); } //罫線用に追加した行・列にRectangleを配置 for (var i = 0; i < ColumnDefinitions.Count; i += 2) { var rectangle = new Rectangle() { Fill = Brushes.Black }; Children.Add(rectangle); SetColumn(rectangle, i); SetRowSpan(rectangle, RowDefinitions.Count); SetZIndex(rectangle, int.MinValue); } for (var i = 0; i < RowDefinitions.Count; i += 2) { var rectangle = new Rectangle() { Fill = Brushes.Black }; Children.Add(rectangle); SetRow(rectangle, i); SetColumnSpan(rectangle, ColumnDefinitions.Count); SetZIndex(rectangle, int.MinValue); } }