Is there a way for an application to know when the report has been printed after being shown in the preview in order for the application to close the dialog box containing the XamReportPreview automatically?
The Report.PrintEnded event works when report.Print() is done but not when used with the preview:
This is fine:
private Report PrepareReport(Visual reportSource) { // 1. Create Report object Report reportObj = new Report();
// 2. Create EmbeddedVisualReportSection section. EmbeddedVisualReportSection section = new EmbeddedVisualReportSection(reportSource);
// 3. Add created section to report's section collection reportObj.ReportSettings.Margin = new Thickness(64); reportObj.ReportSettings.HorizontalPaginationMode = HorizontalPaginationMode.Scale; reportObj.Sections.Add(section);
return reportObj; } // PrepareReport()
private void Print_Executed(object sender, ExecutedRoutedEventArgs e) { // Print the lot list Report report = PrepareReport(this.xamDataGrid_Lots); report.PrintEnded += new EventHandler<Infragistics.Windows.Reporting.Events.PrintEndedEventArgs>(Report_PrintEnded); report.Print(true, true); // show print dialog and progress dialog } // Print_Executed()
private void Report_PrintEnded(object sender, Infragistics.Windows.Reporting.Events.PrintEndedEventArgs args) { MessageBox.Show("Report Done"); }
This doesn't work:
private void PrintPreview_Executed(object sender, ExecutedRoutedEventArgs e) { // Print preview the lot list Report report = PrepareReport(this.xamDataGrid_Lots); PrintPreviewDialog dlg = new PrintPreviewDialog(); dlg.Owner = this; dlg.SetReport(report); dlg.ShowDialog(); } // PrintPreview_Executed()
<Window x:Class="Screens.PrintPreviewDialog" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:igRep="http://infragistics.com/Reporting" Title="Print Preview" Height="{Binding Path=Owner.Height}" Width="{Binding Path=Owner.Width}" MinWidth="300" MinHeight="300" ShowInTaskbar="False"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="*"/> <RowDefinition Height="Auto"/> </Grid.RowDefinitions> <igRep:XamReportPreview x:Name="xamReportPreview" Grid.Row="0"> </igRep:XamReportPreview> <StackPanel Grid.Row="1" Orientation="Horizontal" HorizontalAlignment="Center"> <Button Height="23" Name="btnClose" IsCancel="True" Click="btnClose_Click">Close</Button> </StackPanel> </Grid></Window>
public partial class PrintPreviewDialog : Window { public PrintPreviewDialog() { InitializeComponent(); } // PrintPreviewDialog()
public void SetReport(Report report) { this.xamReportPreview.GeneratePreview(report, false, true); report.PrintEnded += new EventHandler<Infragistics.Windows.Reporting.Events.PrintEndedEventArgs>(Report_PrintEnded); } // SetReport()
private void btnClose_Click(object sender, RoutedEventArgs e) { this.Close(); }
private void Report_PrintEnded(object sender, Infragistics.Windows.Reporting.Events.PrintEndedEventArgs args) { // NEVER CALLED this.Close(); } }
The reason this happens is because the DocumentViewer is handling the Print command and simply printing the fixedpages that were generated for the preview. Unfortunately the DocumentViewer surfaces no events when the printing is complete. If you want to always go through the report's print (which will also allow you to control what dialogs are shown, etc.) then you can handle the PreviewExecuted event and essentially intercept the Print command execution before it gets to the DocumentViewer.
e.g.
Thank you - that worked.