Version

Align Content to the Top of a Page

When you print a report, the EmbeddedVisualReportSection object automatically centers your content on the page. If you want to align your content to the top of the page, you have to create a DataTemplate that does it for you.

Both the Report object and the EmbeddedVisualReportSection object expose a PageContentTemplate property that determines the content layout of the page. If you set the PageContentTemplate property of the Report object, the data template will affect the entire report. However, if you also set an EmbeddedVisualReportSection object’s PageContentTemplate property, it will only affect that particular section.

Aligning content in a report is a two-step process.

  1. Create a DataTemplate object in XAML.

  2. Assign the DataTemplate object to the PageContentTemplate property of a Report object or EmbeddedVisualReportSection object.

The following example code demonstrates how to align content to the top of a page using a data template. The XAML creates a basic DataTemplate object and the procedural code assigns it to the PageContentTemplate property of a Report. The example code assumes you already have a Report object named report1.

In XAML:

<Window.Resources>
    <DataTemplate x:Key="basicContentTemplate">
        <Grid HorizontalAlignment="Left" VerticalAlignment="Top">
            <ContentPresenter Content="{TemplateBinding Content}" />
        </Grid>
    </DataTemplate>
</Window.Resources>

In Visual Basic:

'Find and cast the resource to a DataTemplate
report1.PageContentTemplate = TryCast(Me.TryFindResource("basicContentTemplate"), DataTemplate)

In C#:

//Find and cast the resource to a DataTemplate
report1.PageContentTemplate = this.TryFindResource("basicContentTemplate") as DataTemplate;