wpf - Why do I need to call Dispatcher.BeginInvoke() to allow a visual to properly bind before printing? -
i have usercontrol fixed size of 850x1100 give me same aspect ratio letter-sized piece of paper. display in window inside viewbox
, , acts print preview. control inherits window's datacontext, , when it's displayed on screen, bindings work , looks wonderful.
i've written code below in window's code behind (i feel it's totally view-oriented operation) attempt print it. if execute code written, control prints, not appear data bound.
private void printbutton_click(object sender, routedeventargs e) { var dlg = new printdialog(); var result = dlg.showdialog(); if (result == null || !(bool)result) return; var page = new inspectionformprintview { datacontext = this.datacontext }; page.measure(new size(dlg.printableareawidth, dlg.printableareaheight)); page.arrange(new rect(new point(0, 0), page.desiredsize)); dlg.printvisual(page, "inspection form"); }
if modify last line in method to
dispatcher.begininvoke(new action(() => dlg.printvisual(page, "inspection form")), dispatcherpriority.applicationidle, null);
it print fine. why this?
as mentioned lpl in comments, required because wpf needs perform data bindings. since wpf operations queued on dispatcher
, need queue print operation complete after dispatcherpriority.databind
. therefore, calling begininvoke
dispatcherpriority.render
or lower give wpf time process bindings on control, show in printed output.
Comments
Post a Comment