Important to note: You can use XpsDocumentWriter even when directly printing to a physical printer. Do not make the mistake that I made, avoiding it just because you are not creating a .xps file!
Anyway - I had the same problem and none of the DoEvents() tricks seemed to work. I was also not particularly pleased with the need to use them first. In my situation, some of the data management commands printed well, but some others (nested UserControls) did not. It seemed that only one "level" was data binding, and the rest did not even bind to "DoEvents ()".
The solution was simple, though. Use XpsDocumentWriter as follows. it will open a dialog box in which you can select any physical printer you want.
// 8.5 x 11 paper Size sz = new Size(96 * 8.5, 96 * 11); // create your visual (this is a WPF UserControl) var template = new PackingSlipTemplate() { DataContext = new PackingSlipViewModel(order) }; // arrange template.Measure(sz); template.Arrange(new Rect(sz)); template.UpdateLayout(); // print to XpsDocumentWriter // this will open a dialog and you can print to any installed printer // not just a 'virtual' .xps file PrintDocumentImageableArea area = null; XpsDocumentWriter xps = PrintQueue.CreateXpsDocumentWriter(ref area,); xps.Write(template);
I found the OReilly book in the WPF Programming section โquite useful in the chapterโ Printing - found through Google Books .
If you do not want the print dialog to be displayed, but you want to print directly to the default printer, you can do the following. (For me, the application is designed to print packing lists in a warehouse environment - and I do not want a dialog to appear every time).
var template = new PackingSlipTemplate() { DataContext = new PackingSlipViewModel(orders.Single()) }; // arrange template.Measure(sz); template.Arrange(new Rect(sz)); template.UpdateLayout(); LocalPrintServer localPrintServer = new LocalPrintServer(); var defaultPrintQueue = localPrintServer.DefaultPrintQueue; XpsDocumentWriter xps = PrintQueue.CreateXpsDocumentWriter(defaultPrintQueue); xps.Write(template, defaultPrinter.DefaultPrintTicket);
Simon_Weaver
source share