在我使用C#的程序中,我使用System.Windows.Documents.Table。当我想设置TableCell的垂直对齐时,我发现我不能。它只提供TextAlignment property,它只能设置文本内容的水平对齐方式。有没有任何方法来设置垂直对齐?我会为此感激。
答案 0 :(得分:0)
因为一个TableRow中有多个TableCell。它与situation不同。我的解决方案是遍历每个tablerow,并在每个tablerow中遍历其中的每个tablecell,计算它们中的最大高度作为行的高度,然后根据最大高度设置tablecell的填充。 以下是获取rowheight的方法:
private double getRowHeight(TableRow row)
{
double maxHeight = 0;
foreach (TableCell cell in row.Cells)
{
Rect startRect = cell.ElementStart.GetCharacterRect(LogicalDirection.Forward);
Rect endRect = cell.ElementEnd.GetNextInsertionPosition(LogicalDirection.Backward).GetCharacterRect(LogicalDirection.Forward);
double Height = (endRect.Bottom - startRect.Top);
maxHeight = maxHeight > Height ? maxHeight : Height;
}
return maxHeight;
}
看起来不那么优雅。但这是我能想出的唯一方法。