WPF Print with FlowDocument template

Viewed 115

I´m trying to make some reports on a WPF application but I´m a really beginner on the language. So far I saw some examples on using FlowDocument + PrinDialog to print. Mostly, when loading data from a database, the examples show how to dynamically create the FlowDocument from codebehind, like:

FlowDocument flowDoc = new FlowDocument();
Paragraph paraHeader = new Paragraph();
paraHeader.FontSize = 12;
paraHeader.Foreground = headerBrsh;
paraHeader.FontWeight = FontWeights.Bold;
paraHeader.Inlines.Add(new Run(someDataFromDB));
flowDoc.Blocks.Add(paraHeader);
...

And so on, adding as many items as I need.

What I want to know is if there's anyway to create a FlowDocument XAML template with all the elements and formating and bind the elements text/values to a DataSource that I'll bind when loading the FlowDocument.

Example:

//SomeXAMLTemplate
<FlowDocument>
   <Paragraph FontSize="18" FontWeight="Bold" Background="Black" Foreground="White">
      <Run Text="{Binding SomeProperty}"/>  
   </Paragraph>
</FlowDocument>

And then load it in code like this:

FlowDocument flowDoc = ReadFromXAMLAsFlowDocument(someXAMLFile);
flowDoc.DataContext = myListObject;

I didn't find how to make de XAML properly to get the bindings working.

Could anyone point me the directions or show a working example?

0 Answers
Related