var printItem = new MenuItem { Header = "Print..." };printItem.Click += (sender, e) => {
var reportSection = new EmbeddedVisualReportSection(this); var report = new Report(); report.Sections.Add(reportSection); report.Export(OutputFormat.XPS, @"c:\test.xps"); // Exports fine the entire grid. report.Print(); //throws CrossThread exception
};gridMenu.Items.Add(printItem);
What could be the reason?
The link that you provided accurately explains the problem; there is a bug in the WPF PrintDialog class where it attempts to use the MainWindow which in your case is on another thread. The Report class uses the WPF PrintDialog class when told to show the print dialog so I think the only available workaround until MS fixes the bug in the PrintDialog is to do something like what was marked as an answer in the stackoverflow link you provided. That is, you would show the print dialog using something like the code linked to from that post and then you would set properties on the ReportSettings such as the PrintQueue, PageRange and PageSize.
Thanks.
Hmm .. you 're loosing me here. Could you please point out - may be on the code of my initial post - which lines should be executed in what thread?
Would be great...
Hello Dierk,
The grid can be on any thread. The framework does not allow property access across threads. In this case it is trying to access the Report object in another thread and that is currently not supported. You need to go back to the main thread.
Thanks,
Sam
Sam,
Thanks for your feedback.
However, I'm confused: are you saying that I can't have grids in separate threads, since I can't print them?
This would render the grids useless for us, since our improved app design has multiple UI threads.
Could you please clarify?
Thanks
Dierk,
Grid can run outside the main thread, but if the report object is created on the same thread where the grid is running, then printing the report will fail.
This is a limitation, and I can submit a feature request on your behalf if you prefer.