In my program with C# I use a System.Windows.Documents.Table. When I want to set the vertical alignment of TableCell, I found I cannot. It only provides a TextAlignment property which can only set the horizontal alignment of text content. Is there any method to set its vertical alignment ? I'll be appreciative for this.
Asked
Active
Viewed 946 times
3
-
see 2nd reply here: https://social.msdn.microsoft.com/Forums/vstudio/en-US/2dcb7dee-a467-46af-86f6-dec8cedcb810/table-cell-vertical-alignment?forum=wpf – Nikhil Vartak Nov 02 '15 at 03:16
-
When there is only one TableCell in one TableRow, it works well. But I need more than one TableCells in one TableRow and they all align vertical center. And I cannot know the length of content in the TableCell beforehand because the content will be dynamically generated by the program. @dotnetkid – Frank Nov 02 '15 at 11:02
-
Or is there any way to caculate the actual height of a TableCell ? @dotnetkid – Frank Nov 02 '15 at 12:53
1 Answers
0
Because there is more than one TableCell in one TableRow. It is different to this situation. My solution is to traverse every tablerow, and in every tablerow i traverse every tablecell in it, calculate the max height in them as the height of the row, then set tablecell's padding according to the max height. Here is the method to get the 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;
}
It seems not so elegant. But it's the only method i can figure out.

Frank
- 81
- 6