问题
We're currently using WPF to create a multi-page invoice document, to then be printed / exported via XPS.
The route we've taken to achieve this is to create a UserControl containing a standard ListBox etc displaying the Invoice lines, this is then included in a FlowDocument inside BlockUIContainer tags.
When this FlowDocument is placed inside FlowDocumentScrollViewer tags in a window, it works perfectly, with the databound content of the UserControl displaying properly. However, when we try to create the same FlowDocument in code, it fails with a "'Cannot create unknown type '{clr-namespace:FOO}FooUserControl" XamlParseException. The FlowDocument can be successfully created programmatically if the UserControl is removed.
This is the FlowDocument XAML:
<FlowDocument xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:local="clr-namespace:MARS"
ColumnWidth="400" FontSize="14" FontFamily="Georgia">
<Paragraph>
Blah
</Paragraph>
<BlockUIContainer>
<local:printTestUserControl></local:printTestUserControl>
</BlockUIContainer>
And this is the code we are using to create it in code:
FileStream xamlFile = new FileStream("printTestFlowDoc.xaml", FileMode.Open, FileAccess.Read);
FlowDocument content = (FlowDocument)XamlReader.Load(xamlFile);
flowDocScrollViewer.Document = content;
xamlFile.Close();
The reason we're creating the FlowDocument in code is to then use a Paginator object to slice it into a series of FixedDocuments to then print / export to XPS, none of which we've yet tried, but from what i've read so far it seems like this is a feasible method of achieving multi-page document printing in WPF (Where the document will have a header on the first page, a totals footer on the last, and x pages of rows in between).
Any advice on this issue, or other methods of doing this would be very welcome.
These are some links we've picked up bits of information from so far, sadly not enough though! (I'll include the other links in comments, as StackOverflow doesn't trust me with more than one link at present!)
See the section "Dynamically Creating a FlowDocument, Data Binding and Printing It" Scott Hanselmann seemed to have the same problem as we did, but adding the line
Dispatcher.CurrentDispatcher.Invoke(DispatcherPriority.SystemIdle, new DispatcherOperationCallback(delegate { return null; }), null);
didn't get our FlowDocument to load, however he was only binding to TextBlocks in his FlowDocument, rather than including a UserControl.
Thanks very much for reading this far! and for any help anyone can offer.
回答1:
Sorry i've been so slow in tidying up this question, but we've just finished this project, and i'm now tidying up all the loose ends from it.
It turned out that the flowdocument / XPS method was a completely wrong headed way of approaching this task, and in fact the built in RDLC reports allowed us to achieve everything we needed for our invoice documents, in a relatively straight forward manner.
The main bonus of this was that we were able to report on in-memory instances of our business model objects, rather than having to use data looked up from a db, via stored procs called from crystal, which the previous system relied upon. The report file editor in VS was simple to get to grips with, if at times a little irritating when laying out complex reports.
Hopefully this may be of some use to people considering their options when it comes to reporting from WPF applications.
来源:https://stackoverflow.com/questions/3931925/wpf-printing-multipage-invoice-via-flowdocument-paginator-and-fixeddocument