WPF Flow Document:在代码后面添加时调整单元格高度

时间:2016-07-06 09:17:48

标签: c# wpf xaml flowdocument

我有一个带有表格的FlowDocument。当我直接在XAML中创建表格时,当一些单元格内容被分成两行时,所有单元格都自动适合,整个行的高度相等。

<TableRow Name="TestRow">
                        <TableCell BorderThickness="1,0,1,1" BorderBrush="LightGray">
                            <Paragraph>1</Paragraph>
                        </TableCell>
                        <TableCell BorderThickness="0,0,1,1" BorderBrush="LightGray">
                            <Paragraph>This is sentence which is divided into two lines</Paragraph></TableCell>
                        <TableCell BorderThickness="0,0,1,1" BorderBrush="LightGray">
                            <Paragraph>123</Paragraph></TableCell>
                        <TableCell></TableCell>
                    </TableRow>

enter image description here

但是当我想在后面的代码中动态创建表时,单元格具有不同的高度。

tableProducts.RowGroups[0].Rows.Add(new TableRow());
        TableRow currentRow = tableProducts.RowGroups[0].Rows[1];
        currentRow.Cells.Add(new TableCell(new Paragraph(new Run("1")) { BorderThickness = new Thickness(1, 0, 0, 1), BorderBrush = Brushes.LightGray }));
        currentRow.Cells.Add(new TableCell(new Paragraph(new Run("This is sentence which is divided into two lines")) { BorderThickness = new Thickness(1, 0, 1, 1), BorderBrush = Brushes.LightGray }));
        currentRow.Cells.Add(new TableCell(new Paragraph(new Run("123")) { BorderThickness = new Thickness(1, 0, 0, 1), BorderBrush = Brushes.LightGray }));

enter image description here

我无法找到任何有关如何在后面的代码中创建单元格高度时对齐的解决方案。有没有办法解决这个问题?

1 个答案:

答案 0 :(得分:0)

new TableCell(new Paragraph(...) { BorderThickness = ... })更改为new TableCell(new Paragraph(...)) { BorderThickness = ... }

原因是Paragraph不占用TableCell的所有空格,其边界位于单元格内。但是如果你试图绘制TableCell的边框,一切都会正常工作。