Search code examples
c#.netflowdocument

TableCell/TableRow Margin programmatically


This has to be something super simple but it is driving me insane. I have a Table that is created programmatically. The Table has multiple rows and I want to control the spacing between each row. I figured that changing the Paragraph Margin would do the trick but the Cell still has a margin. This is what I am doing;

Table tb = new Table();
tb.Columns.Add(new TableColumn());
tb.RowGroups.Add(new TableRowGroup());
TableRow tr = new TableRow();
Paragraph p1 = new Paragraph(new Run("A"));
p1.Margin = new Thickness(0);
tr.Cells.Add(new TableCell(p1));
tb.RowGroups[0].Rows.Add(tr);
tr = new TableRow();
p1 = new Paragraph(new Run("A"));
p1.Margin = new Thickness(0);
tr.Cells.Add(new TableCell(p1));
tb.RowGroups[0].Rows.Add(tr);
flowDoc.Blocks.Add(tb);

I tried to create a TableCell object and set the Padding = new Thickness(0); but this did not have any effect.

Note: I can be hard to see the extra space. If you add two Paragraph with a Margin of 0 you will see in fact there is space difference;

p1 = new Paragraph(new Run("B"));
p1.Margin = new Thickness(0);
flowDoc.Blocks.Add(p1);
p1 = new Paragraph(new Run("B"));
p1.Margin = new Thickness(0);
flowDoc.Blocks.Add(p1);

enter image description here

edit: it was suggested it was an issue with different sizes in the numbers verse letters. So I changed the both examples to be letters to show the same thing occurs.

edit 2: reply to Ed Plunkett: Your XMAL example does me no good as this needs to be programmatically created. However, I did take your idea and does make it much clearer that the cell or the row is adding a space. I added a red to the first Paragraph than gold to the second;

enter image description here


Solution

  • I bet it's a simple fix:

    Table tb = new Table() { CellSpacing = 0 };
    

    And here's my reasoning. It looks like Table.CellSpacing defaults to 2:

    <Grid>
        <Grid.RenderTransform>
            <ScaleTransform ScaleX="2" ScaleY="2" />
        </Grid.RenderTransform>
        <FlowDocumentScrollViewer>
            <FlowDocument>
                <FlowDocument.Blocks>
                    <Table x:Name="MyTable">
                        <Table.Columns>
                            <TableColumn />
                        </Table.Columns>
                        <Table.RowGroups>
                            <TableRowGroup>
                                <TableRow>
                                    <TableCell Background="Pink">
                                        <Paragraph Background="LightBlue">
                                            <Run Background="PaleGoldenrod">0123456789Aj</Run>
                                        </Paragraph>
                                    </TableCell>
                                </TableRow>
                                <TableRow>
                                    <TableCell Background="Pink">
                                        <Paragraph Background="LightBlue">
                                            <Run Background="PaleGoldenrod">0123456789Aj</Run>
                                        </Paragraph>
                                    </TableCell>
                                </TableRow>
                            </TableRowGroup>
                        </Table.RowGroups>
                    </Table>
                    <Paragraph Background="LightSkyBlue" Margin="0">
                        <Run>0123456789Aj</Run>
                    </Paragraph>
                    <Paragraph Background="LightSkyBlue" Margin="0">
                        <Run>0123456789Aj</Run>
                    </Paragraph>
                </FlowDocument.Blocks>
            </FlowDocument>
        </FlowDocumentScrollViewer>
        <Label Content="{Binding CellSpacing, ElementName=MyTable}" />
    </Grid>
    

    enter image description here

    So if I set it to zero in XAML...

    <Table x:Name="MyTable" CellSpacing="0">
    

    enter image description here