1
在我的C#程序中,我使用了一個System.Windows.Documents.Table。當我想要設置TableCell的垂直對齊時,我發現我不能。它只提供TextAlignment property,它只能設置文本內容的水平對齊。是否有任何方法來設置其垂直對齊?我會爲此感激。如何設置System.Windows.Documents.TableCell的垂直對齊方式?
在我的C#程序中,我使用了一個System.Windows.Documents.Table。當我想要設置TableCell的垂直對齊時,我發現我不能。它只提供TextAlignment property,它只能設置文本內容的水平對齊。是否有任何方法來設置其垂直對齊?我會爲此感激。如何設置System.Windows.Documents.TableCell的垂直對齊方式?
因爲在一個TableRow中有多個TableCell。這與situation不同。我的解決方案是遍歷每個表格,並且在每個表格中,我遍歷每個表格單元格,計算其中的最大高度作爲行的高度,然後根據最大高度設置表格填充。 這裏是讓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;
}
似乎不那麼優雅。但這是我能弄清楚的唯一方法。
請參閱第2次答覆:https://social.msdn.microsoft.com/Forums/vstudio/en-US/2dcb7dee-a467-46af-86f6-dec8cedcb810/table-cell-vertical-alignment?forum=wpf – niksofteng
當一個TableRow中只有一個TableCell時,它運行良好。但是我需要在一個TableRow中使用多個TableCells,並且它們都對齊垂直中心。我無法預先知道TableCell中內容的長度,因爲內容將由程序動態生成。 @dotnetkid – Frank
或者有什麼辦法來計算TableCell的實際高度? @dotnetkid – Frank