如何设置System.Windows.Documents.TableCell的垂直对齐方式?
How to set the vertical alignment of a System.Windows.Documents.TableCell?
在我的 C# 程序中,我使用 System.Windows.Documents.Table。当我想设置TableCell的垂直对齐时,我发现我不能。它只提供了一个TextAlignment property,只能设置文本内容的水平对齐方式。有什么方法可以设置它的垂直对齐方式吗?我会很感激这一点。
因为一个TableRow中有多个TableCell。与此不同situation。我的解决方案是遍历每一个tablerow,在每一个tablerow中我遍历其中的每一个tablecell,计算其中的最大高度作为行的高度,然后根据最大高度设置tablecell的padding。
下面是获取行高的方法:
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;
}
好像不太优雅。但这是我唯一能想到的方法。
在我的 C# 程序中,我使用 System.Windows.Documents.Table。当我想设置TableCell的垂直对齐时,我发现我不能。它只提供了一个TextAlignment property,只能设置文本内容的水平对齐方式。有什么方法可以设置它的垂直对齐方式吗?我会很感激这一点。
因为一个TableRow中有多个TableCell。与此不同situation。我的解决方案是遍历每一个tablerow,在每一个tablerow中我遍历其中的每一个tablecell,计算其中的最大高度作为行的高度,然后根据最大高度设置tablecell的padding。 下面是获取行高的方法:
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;
}
好像不太优雅。但这是我唯一能想到的方法。