How can I use a WPF FlowDocument or FixedDocument to compose a printable page from on screen controls?
开发者_运维问答I'm attempting to print a number of WPF controls (datagrid, charts etc) that are on screen in my application but which need to be re-arranged and supplemented with other text etc.
Using the WPF FlowDocument or FixedDocument seems the obvious way to compose and print documents but I'm struggling to achieve this.
I've tried creating a VisualBrush from the control(s) and using this as the Fill on a Rectangle without success (the Rectangle is not displayed at all).
Has anyone used FlowDocument/FixedDocument to do this and/or have any example code showing how it's achieved?
Found that it works okay if the FlowDocument is rendered before printing (e.g. if hosted in a FlowDocumentReader).
XAML
xmlns:d3="http://research.microsoft.com/DynamicDataDisplay/1.0"
.
<d3:ChartPlotter x:Name="plotter">
<d3:Header TextBlock.FontSize="20">
Very simple chart
</d3:Header>
</d3:ChartPlotter>
.
<FlowDocumentReader>
<FlowDocument>
<Paragraph>
<Run>Graph Header</Run>
</Paragraph>
<BlockUIContainer>
<Rectangle x:Name="GraphRectangle" Height="300"/>
</BlockUIContainer>
</FlowDocument>
</FlowDocumentReader>
CS
public MainWindow()
{
InitializeComponent();
GraphRectangle.Fill = new VisualBrush(plotter);
}
精彩评论