由于某些原因,标题和其余部分之间的行未对齐。 我已经检查了边框,标签和网格列上的宽度,它们应该对齐。我想念什么吗?
这是我后面的代码
public static void CreateGrid(Grid container, List<GridColumnItem> labels, Thickness rowMargins, int paddingTop = 65, int headerFontSize = 24)
{
var pos = new Point(Xpos(1, 1), Ypos(1, 1, paddingTop));
double rowHeight = 0;
container.Background = new SolidColorBrush(Color.FromRgb(154, 32, 39));
container.RowDefinitions.Add(new RowDefinition());
container.VerticalAlignment = VerticalAlignment.Top;
container.HorizontalAlignment = HorizontalAlignment.Left;
container.Width = partWidth(1) - pos.X - 10;
container.Margin = new Thickness(pos.X, pos.Y, 10, 0);
//Create columns first to get widths
foreach (var item in labels)
{
for (int i = 0; i < item.columnSpan; i++)
{
container.ColumnDefinitions.Add(new ColumnDefinition());
container.ColumnDefinitions[container.ColumnDefinitions.Count - 1].Width = new GridLength(item.columnWidth / item.columnSpan);
}
}
var colCnt = 0;
container.Measure(new Size(partWidth(1) - 20, partHeight(1, paddingTop)));
//Populate Headers
for (int i = 0; i < labels.Count; i++)
{
var item = labels[i];
var newLabel = MakeDefaultTextBlock(TextAlignment.Center, headerFontSize, Brushes.Black, "");
newLabel.Inlines.Add(new Bold(new Run(item.headerCaption)));
newLabel.Margin = rowMargins;
newLabel.Width = container.ColumnDefinitions[colCnt].Width.GridUnitType == GridUnitType.Pixel ? (container.ColumnDefinitions[colCnt].Width.Value * item.columnSpan) - (rowMargins.Left + rowMargins.Right) : (container.DesiredSize.Width / container.ColumnDefinitions.Count) - (rowMargins.Left + rowMargins.Right);
//newLabel.Width = container.DesiredSize.Width / container.ColumnDefinitions.Count;
newLabel.Measure(new Size(Double.PositiveInfinity, Double.PositiveInfinity));
rowHeight = Math.Max(rowHeight, newLabel.DesiredSize.Height);
var border = new Border();
border.HorizontalAlignment = HorizontalAlignment.Left;
border.VerticalAlignment = VerticalAlignment.Top;
border.Margin = new Thickness(0);
border.Height = rowHeight + 10;
border.Width = (int)(container.ColumnDefinitions[colCnt].Width.Value * item.columnSpan);
border.BorderThickness = new Thickness((i == 0 ? 2 : 0), 2, 2, 0);
border.BorderBrush = Brushes.Black;
border.Child = newLabel;
border.Measure(new Size(Double.PositiveInfinity, Double.PositiveInfinity));
Grid.SetColumn(border, colCnt);
Grid.SetRow(border, 0);
Grid.SetColumnSpan(border, item.columnSpan);
container.Children.Add(border);
colCnt++;
}
container.RowDefinitions[container.RowDefinitions.Count - 1].Height = new GridLength(rowHeight + 10);
container.Height = rowHeight + 15;
}
public static void GridAddRow(Grid grid, List<GridCellItem> values, Thickness rowMargins, bool Lastrow = false)
{
var started = false;
if (values.Count > 0)
{
double rowHeight = 0;
grid.RowDefinitions.Add(new RowDefinition());
var rowNum = grid.RowDefinitions.Count;
foreach (var item in values)
{
if (item.IsntBlank())
{
started = true;
if (item.captionText.IsntBlank())
{
var newLabel = MakeDefaultTextBlock(item.hAlign, item.captionFontSize, item.captionForeColor, item.captionText);
newLabel.Margin = rowMargins;
newLabel.Width = grid.ColumnDefinitions[item.columnNo].Width.GridUnitType == GridUnitType.Pixel ? grid.ColumnDefinitions[item.columnNo].Width.Value - (rowMargins.Left + rowMargins.Right) : (grid.DesiredSize.Width / grid.ColumnDefinitions.Count) - (rowMargins.Left + rowMargins.Right);
newLabel.Measure(new Size(Double.PositiveInfinity, Double.PositiveInfinity));
rowHeight = Math.Max(rowHeight, newLabel.DesiredSize.Height);
var border = new Border();
border.HorizontalAlignment = HorizontalAlignment.Left;
border.VerticalAlignment = VerticalAlignment.Top;
//border.Margin = new Thickness(0);
border.Height = rowHeight + 10;
//border.Width = grid.ColumnDefinitions[item.columnNo].Width.Value;
border.BorderThickness = item.border;
border.BorderBrush = Brushes.Black;
border.Measure(new Size(Double.PositiveInfinity, Double.PositiveInfinity));
border.Child = newLabel;
Grid.SetColumn(border, item.columnNo);
Grid.SetRow(border, grid.RowDefinitions.Count - 1);
if (item.rEvent.IsntBlank())
{
newLabel.MouseUp += item.rEvent;
}
newLabel.Tag = item.Tag;
grid.Children.Add(border);
}
else
{
var border = new Border();
border.HorizontalAlignment = HorizontalAlignment.Left;
border.VerticalAlignment = VerticalAlignment.Top;
border.Margin = new Thickness(0);
border.Height = rowHeight + 10;
border.Width = grid.ColumnDefinitions[item.columnNo].Width.Value;
border.BorderThickness = item.border;
border.BorderThickness = new Thickness(1);
border.BorderBrush = Brushes.Black;
Grid.SetColumn(border, item.columnNo);
Grid.SetRow(border, grid.RowDefinitions.Count - 1);
grid.Children.Add(border);
}
}
else
{
var border = new Border();
border.HorizontalAlignment = HorizontalAlignment.Left;
border.VerticalAlignment = VerticalAlignment.Top;
border.Margin = new Thickness(0);
border.Height = rowHeight + 10;
border.Width = grid.ColumnDefinitions[item.columnNo].Width.Value;
border.BorderThickness = item.border;
border.BorderBrush = Brushes.Black;
Grid.SetColumn(border, item.columnNo);
Grid.SetRow(border, grid.RowDefinitions.Count - 1);
grid.Children.Add(border);
}
}
grid.RowDefinitions[grid.RowDefinitions.Count - 1].Height = new GridLength(rowHeight + 10);
grid.Height = grid.Height + rowHeight + (Lastrow ? 0 : 15);
}
}
我正在使用此代码执行
var gridColumns = new List<GridColumnItem>() {
new GridColumnItem("Item Type", (int)(211.0 / 1260 * (screenSize.Width - 20)), 1),
new GridColumnItem("Description", (int)(318.0 / 1260 * (screenSize.Width - 20)), 1),
new GridColumnItem("Account", (int)(105.0 / 1260 * (screenSize.Width - 20)), 1),
new GridColumnItem("Qty", (int)(100.0 / 1260 * (screenSize.Width - 20)), 1),
new GridColumnItem("Amount", (int)(207.0 / 1260 * (screenSize.Width - 20)), 1),
new GridColumnItem("Actions", (int)(319.0 / 1260 * (screenSize.Width - 20)), 3) };
CreateGrid(checkout.checkoutGrid, gridColumns, new Thickness(5, 5, 5, 5));
foreach (var item in checkItems)
{
GridAddRow(checkout.checkoutGrid, new List<GridCellItem>()
{ new GridCellItem("Item Type", $"{item.ItemType}", 24, 0, new Thickness(2, 2, 0, 0)) { hAlign = TextAlignment.Left, captionForeColor = Brushes.Wheat },
new GridCellItem("Description", $"{item.ItemDescription}", 24, 1, new Thickness(2, 2, 0, 0)){ hAlign = TextAlignment.Left, captionForeColor = Brushes.Wheat },
new GridCellItem("Account", $"{item.ItemAccount}", 24, 2, new Thickness(2, 2, 0, 0)){ hAlign = TextAlignment.Left, captionForeColor = Brushes.Wheat },
new GridCellItem("Qty", $"{item.ItemQty}", 24, 3, new Thickness(2, 2, 0, 0)) { hAlign = TextAlignment.Right, captionForeColor = Brushes.Wheat },
new GridCellItem("Amount", $"{item.ItemAmount:c}", 24, 4, new Thickness(2, 2, 0, 0)) { hAlign = TextAlignment.Right, captionForeColor = Brushes.Wheat },
new GridCellItem("Remove", $"Remove", 24, 5, new Thickness(2, 2, 0, 0)) { hAlign = TextAlignment.Center, captionForeColor = Brushes.White, rEvent = removeItem_Click, Tag = item.ID },
new GridCellItem("Query", $"Query", 24, 6, new Thickness(2, 2, 0, 0)) { hAlign = TextAlignment.Center, captionForeColor = Brushes.White, rEvent = removeItem_Click, Tag = item.ID },
new GridCellItem("View", $"View", 24, 7, new Thickness(2, 2, 2, 0)) { hAlign = TextAlignment.Center, captionForeColor = Brushes.White, rEvent = removeItem_Click, Tag = item.ID }
},
new Thickness(5, 5, 5, 5)
);
RowCnt++;
}
GridAddRow(checkout.checkoutGrid, new List<GridCellItem>()
{
new GridCellItem("Total", 36, 3, new Thickness(2, 2, 0, 2)),
new GridCellItem($"{checkItems.Sum(x => x.ItemAmount):c}", 36, 4, new Thickness(2, 2, 2, 2)) { hAlign = TextAlignment.Right },
new GridCellItem(0, new Thickness(2, 2, 0, 2)),
new GridCellItem(1, new Thickness(0, 2, 0, 2)),
new GridCellItem(2, new Thickness(0, 2, 0, 2)),
new GridCellItem(5, new Thickness(0, 2, 0, 2)),
new GridCellItem(6, new Thickness(0, 2, 0, 2)),
new GridCellItem(7, new Thickness(0, 2, 2, 2)),
},
new Thickness(5, 5, 5, 5), true);