Version

Handling Events to Customize Print Operations

Before You Begin

The WPF Reporting engine prints/exports the DataPresenter control’s state exactly as-is when you invoke the Print or Export method of the Report object. The resulting printout or exported file is based on how your end users interact with the DataPresenter control before printing or exporting.

Fortunately, the EmbeddedVisualReportSection object exposes paging-related events that you can handle to retrieve a reference to a copy of the DataPresenter control that the WPF Reporting engine is printing/exporting. You can use this reference to set the DataPresenter control’s properties or to attach event handlers to events exposed by the DataPresenter control. In particular, you can handle the FieldLayoutInitialized event or the InitializeRecord event to set field/field layout properties or record properties, respectively. Since the reference to the DataPresenter control is a copy, setting properties on the copy will not modify the DataPresenter control on the user interface.

What You Will Accomplish

You will handle a pair of events off the EmbeddedVisualReportSection object to get a reference to the copy of the xamDataGrid control that the WPF Reporting engine is going to print/export. Once you have a reference to the DataPresenter control in the event handlers, you will add an event handler to the DataPresenter control’s InitializeRecord event so you can hide every even-numbered record. Finally, you will remove the event handlers for DataPresenter control’s InitializeRecord event, as well as the pair of events for the EmbeddedVisualReportSection object.

The example code also refers to the xamDataGrid control when creating an EmbeddedVisualReportSection object; however, you can replace that with the xamDataPresenter control or the xamDataCarousel control. The example code does not demonstrate or explain binding xamDataGrid to hierarchical data, Adding xamDataGrid to Your Page, or the individual steps for Adding WPF Reporting to Your Page using the xamDataGrid control.

Follow these Steps

  1. Open the code-behind and place using/Imports directives in your code-behind so you don’t have to type out a member’s fully qualified name.

    In Visual Basic:

    Imports Infragistics.Windows.Reporting
    Imports Infragistics.Windows.Reporting.Events
    Imports Infragistics.Windows.DataPresenter
    Imports Infragistics.Windows.DataPresenter.Events

    In C#:

    using Infragistics.Windows.Reporting;
    using Infragistics.Windows.Reporting.Events;
    using Infragistics.Windows.DataPresenter;
    using Infragistics.Windows.DataPresenter.Events;
  1. Create a report using a xamDataGrid control.

    The example code assumes your using a xamDataGrid control is named xamDataGrid1.

    Note
    Note

    With the exception of the event handlers (e.g., PaginationStarting, PaginationEnded, InitializeRecord), you can add the example code inside an event handler such as a Button control’s click event.

    In Visual Basic:

    Private Sub Button_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
        Dim report1 As New Report()
        Dim section1 As New EmbeddedVisualReportSection(Me.xamDataGrid1)
        report1.Sections.Add(section1)
        'TODO: Attach event handlers to section1
        'TODO: Print or export the report
        'TODO: Remove event handlers from section1
    End Sub

    In C#:

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        Report report1 = new Report();
        EmbeddedVisualReportSection section1 = new EmbeddedVisualReportSection(this.xamDataGrid1);
        report1.Sections.Add(section1);
        //TODO: Attach event handlers to section1
        //TODO: Print or export the report
        //TODO: Remove event handlers from section1
    }
  1. Attach an event handler to the EmbeddedVisualReportSection object’s PaginationStarting event.

    The EmbeddedVisualReportSection object raises the PaginationStarting event when you invoke the Report object’s Print or Export method.

    In Visual Basic:

    AddHandler section1.PaginationStarting, New EventHandler(Of EmbeddedVisualPaginationEventArgs)(AddressOf section1_PaginationStarting)

    In C#:

    section1.PaginationStarting += new EventHandler<EmbeddedVisualPaginationEventArgs>(section1_PaginationStarting);
  1. Attach an event handler to the EmbeddedVisualReportSection object’s PaginationEnded event.

    In Visual Basic:

    AddHandler section1.PaginationEnded, New EventHandler(Of EmbeddedVisualPaginationEventArgs)(AddressOf section1_PaginationEnded)

    In C#:

    section1.PaginationEnded += new EventHandler<EmbeddedVisualPaginationEventArgs>(section1_PaginationEnded);
  1. Print the report.

    In Visual Basic:

    report1.Print()

    In C#:

    report1.Print();
  1. Remove the event handler for the PaginationStarting event from the EmbeddedVisualReportSection object.

    In Visual Basic:

    RemoveHandler section1.PaginationStarting, AddressOf section1_PaginationStarting

    In C#:

    section1.PaginationStarting -= section1_PaginationStarting;
  1. Remove the event handler for the PaginationEnded event from the EmbeddedVisualReportSection object.

    In Visual Basic:

    RemoveHandler section1.PaginationEnded, AddressOf section1_PaginationEnded

    In C#:

    section1.PaginationEnded -= section1_PaginationEnded;
  1. Locate the PaginationStarting event handler.

    In Visual Basic:

    Private Sub section1_PaginationStarting(ByVal sender As Object, ByVal e As EmbeddedVisualPaginationEventArgs)
        'TODO: TryCast e.VisualPaginator to a DataPresenterBase class
        'TODO: Attach events to the xamDataGrid control
    End Sub

    In C#:

    private void section1_PaginationStarting(object sender, EmbeddedVisualPaginationEventArgs e)
    {
        //TODO: Try to cast e.VisualPaginator to a DataPresenterBase class
        //TODO: Attach events to the xamDataGrid control
    }
  1. Try to cast the EmbeddedVisualPaginationEventArgs object’s VisualPaginator property to a DataBasePresenter class.

    In Visual Basic:

    Dim presenter As DataPresenterBase = TryCast(e.VisualPaginator, DataPresenterBase)

    In C#:

    DataPresenterBase presenter = e.VisualPaginator as DataPresenterBase;
  1. If the cast was successful, attach an event handler to the DataPresenterBase object’s InitializeRecord event.

    You can also attach event handlers to other events exposed by the DataPresenterBase class.

    In Visual Basic:

    If presenter IsNot Nothing
        AddHandler presenter.InitializeRecord, New EventHandler(Of InitializeRecordEventArgs)(AddressOf presenter_InitializeRecord)
        'Note: You can also attach event handlers to other events exposed by the DataPresenterBase class.
    End If

    In C#:

    if(presenter != null)
    {
        presenter.InitializeRecord += new EventHandler<InitializeRecordEventArgs>(presenter_InitializeRecord);
        //Note: You can also attach event handlers to other events exposed by the DataPresenterBase class.
    }
  1. Locate the PaginationEnded event handler.

    In Visual Basic:

    Private Sub section1_PaginationEnded(ByVal sender As Object, ByVal e As EmbeddedVisualPaginationEventArgs)
        'TODO: Try to cast e.VisualPaginator to a DataPresenterBase class
        'TODO: If the cast is successful, remove event handlers from the DataPresenterBase object.
    End Sub

    In C#:

    private void section1_PaginationEnded(object sender, EmbeddedVisualPaginationEventArgs e)
    {
        //TODO: Try to cast e.VisualPaginator to a DataPresenterBase class.
        //TODO: If the cast is successful, remove event handlers from the DataPresenterBase object.
    }
  1. Try to cast the EmbeddedVisualPaginationEventArgs object’s VisualPaginator property to a DataPresenterBase class.

    In Visual Basic:

    Dim presenter As DataPresenterBase = TryCast(e.VisualPaginator, DataPresenterBase)

    In C#:

    DataPresenterBase presenter = e.VisualPaginator as DataPresenterBase;
  1. If the cast was successful, remove the event handler from the DataPresenterBase object’s InitializeRecord event.

    If you have registered for any other events exposed by the DataPresenterBase class, remove the event handlers from those events as well.

    In Visual Basic:

    If presenter IsNot Nothing
        RemoveHandler presenter.InitializeRecord, AddressOf presenter_InitializeRecord
        'Make sure you remove event handlers from the events exposed by the DataPresenterBase class
    End If

    In C#:

    if (presenter != null)
    {
        presenter.InitializeRecord -= presenter_InitializeRecord;
        //Make sure you remove event handlers from the events exposed by the DataPresenterBase class
    }
  1. Locate the InitializeRecord event handler.

    In Visual Basic:

    Private Sub presenter_InitializeRecord(ByVal sender As Object, ByVal e As InitializeRecordEventArgs)
        'TODO: Expand records whose index % 2 is 0
    End Sub

    In C#:

    private void presenter_InitializeRecord(object sender, InitializeRecordEventArgs e)
    {
        //TODO: Expand records whose index % 2 is 0
    }
  1. If the record is an even-numbered record, hide it by setting its Visibility property to Collapsed.

    In Visual Basic:

    If e.Record.Index Mod 2 = 0 Then
        e.Record.Visibility = Visibility.Collapsed
    End If

    In C#:

    if (e.Record.Index % 2 == 0)
    {
        e.Record.Visibility = Visibility.Collapsed;
    }
  1. Run your project.

    When you print the xamDataGrid control, the WPF Reporting engine will only print the odd-numbered records.