Question

I have an issue regarding a TableCell splitting strategy on WPF FlowDocument Table.

Here is a simple code allowing to reproduce the issue :

MainWindow.xaml.cs

/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        var table = new Table() { BorderThickness = new Thickness(1), BorderBrush = Brushes.Black, CellSpacing = 0 };
        var rowGroup = new TableRowGroup();
        var tableRow = new TableRow();
        var cell1 = new TableCell() { Background = Brushes.Red, BorderThickness = new Thickness(0, 0, 1, 0), BorderBrush = Brushes.Black };
        var cell2 = new TableCell() { Background = Brushes.Red };

        cell1.Blocks.Add(new Paragraph(new Run("Cell 1 ******************************************************************************")));
        cell2.Blocks.Add(new Paragraph(new Run("Cell 2")));
        tableRow.Cells.Add(cell1);
        tableRow.Cells.Add(cell2);
        rowGroup.Rows.Add(tableRow);
        table.RowGroups.Add(rowGroup);

        var flowDocument = new FlowDocument();
        flowDocument.Blocks.Add(table);

        Content = flowDocument;
    }
}

And here is the result :

TableCell Split

As you can see on the second page, the right cell Background color is lost.

Has anyone already came across this issue? Any solution/workaround will be welcome!

Edit 1 : All properties are lost so setting the Background color on the Row/Column won't solve my problem (I have mainly issues regarding TableCell Border Thicknesses)

Here is a screen showing the issue with borders :

enter image description here

Edit 2 : Looking at the Visual Tree is fairly instructive. The pagination process seems to only generates one ParagraphVisual for the Row on the second page, thus explaining the loss of all visual effects. There is no Visual, and thus no background/borders/etc... A solution may be to tweak the DocumentPaginator associated to the FlowDocument

enter image description here

Was it helpful?

Solution 2

Sadly, I was not able to find a solution. This seems to be a bug inherent to the WPF FlowDocument and it is not easy to find an entry point in the pagination process.

My main goal was to have Tables splitting correctly among pages in my document so I finally decided to allow Table to split, but not Cells.

This was quite easy to do, I just had to wrap my cell contents in a BlockUIContainer like this :

cell1.Blocks.Add(new BlockUIContainer() { Child = new TextBlock () { Text = "Cell 1 ******************************************************************************", TextWrapping = TextWrapping.Wrap}});

This allows me to avoid having missing borders in split tables, but cells cannot split anymore.

This is not satisfactory but is the best I was able to achieve.

OTHER TIPS

I've changed your code to demonstrate even more what Eyal H has stated:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        var table = new Table() { BorderThickness = new Thickness(1), BorderBrush = Brushes.Black, CellSpacing = 4 };
        var rowGroup = new TableRowGroup();
        var tableRow = new TableRow();
        var cell1 = new TableCell() { RowSpan = 1, Background = Brushes.Red, BorderThickness = new Thickness(3, 3, 3, 3), BorderBrush = Brushes.Green };
        var cell2 = new TableCell() { RowSpan = 1, Background = Brushes.Red, BorderThickness = new Thickness(2, 2, 2, 2), BorderBrush = Brushes.Blue };

        var correctContent = "**************************************************************************************************************************************************************************************************************************************";

        cell1.Blocks.Add(new Paragraph(new Run("Cell 1" + correctContent)));
        cell2.Blocks.Add(new Paragraph(new Run("Cell 2" + correctContent.Replace("*","   ")+".")));
        tableRow.Cells.Add(cell1);
        tableRow.Cells.Add(cell2);
        rowGroup.Rows.Add(tableRow);
        table.RowGroups.Add(rowGroup);

        var flowDocument = new FlowDocument();
        flowDocument.Blocks.Add(table);

        Content = flowDocument;
    }
}

The Cell 2 has many spaces ending with a dot. Cell 1 fits on page 1 and an empty cell with no borders (and no colour) is placed on the second page. I was not able to find a property of any Table or DocumentPaginator object in the hierarchy that would deal with the page breaks within cells.

Cell 1 made shorter

"Since the Cell2 is shorter than the page it does not split and therefore an "empty" cell is located at the second page (with no properties)"

Maybe it's time for a Connect article?

I think adding TableColumn will solve your problem. below is the sample code.

var table = new Table() { BorderThickness = new Thickness(1), BorderBrush = Brushes.Black, CellSpacing = 0 };
        var rowGroup = new TableRowGroup();
        var tableRow = new TableRow();
        var tableColumn1 = new TableColumn { Background = Brushes.Red };
        var tableColumn2 = new TableColumn { Background = Brushes.Red };
        var cell1 = new TableCell() { Background = Brushes.Red, BorderThickness = new Thickness(0, 0, 1, 0), BorderBrush = Brushes.Black };
        var cell2 = new TableCell() { Background = Brushes.Red };

        cell1.Blocks.Add(new Paragraph(new Run("Cell 1 ******************************************************************************")));
        cell2.Blocks.Add(new Paragraph(new Run("Cell 2")));
        tableRow.Cells.Add(cell1);
        tableRow.Cells.Add(cell2);
        rowGroup.Rows.Add(tableRow);
        table.Columns.Add(tableColumn1);
        table.Columns.Add(tableColumn2);
        table.RowGroups.Add(rowGroup);

        var flowDocument = new FlowDocument();
        flowDocument.Blocks.Add(table);

        Content = flowDocument;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top