I am building up data in a flow document. The user can alter some properties and see a Live "Print Preview". I do this by simply converting the FlowDocument to a Fixed Document (using the following method from this post) and display in a DocumentViewer.
public static FixedDocument ToFixedDocument(this FlowDocument flowDocument)
{
var paginator = ((IDocumentPaginatorSource)flowDocument).DocumentPaginator;
var package = Package.Open(new MemoryStream(), FileMode.Create, FileAccess.ReadWrite);
var packUri = new Uri("pack://temp.xps");
PackageStore.RemovePackage(packUri);
PackageStore.AddPackage(packUri, package);
var xps = new XpsDocument(package, CompressionOption.NotCompressed, packUri.ToString());
XpsDocument.CreateXpsDocumentWriter(xps).Write(paginator);
FixedDocument doc = xps.GetFixedDocumentSequence().References[0].GetDocument(true);
return doc;
}
However any datagrids that have been added to the FlowDocument are not being rendered correctly in the FixedDocument. They appear in the FixedDocument with no data; the shape of the grid is correct, with column headers and rows, just no data.
Originally I thought it might have been because the datagrids are contained within a BlockUIContainer in the FlowDocument, however I have other BlockUIContainer elements containing a Grid (with TextBlocks) and even an Xceed DataGridControl. They all render just fine in the FixedDocument.
I see there are other questions elsewhere about displaying DataGrids, but they convert the DataGrid to Tables, or create a DocumentPaginator to repaginate a datagrid when it spans multiple pages. Neither of these work for me, I want to see the DataGrid in the FixedDocument exactly as it appears in the FlowDocument
Why is the DataGrid not displaying correctly in the FixedDocument?