Example E2942
Visible to All Users

Reporting for WPF - Create a Document Sections Based on the Input Values

This example illustrates how to generate a document according to user input. To pass specific values to the document's detail section, assign these values to the e.Data parameter of the SimpleLink.CreateDetail event. As for the report page header (footer) sections, override their DataContext that is accessible through the corresponding Link properties. For instance, use the TemplatedLink.PageHeaderData property to set the data context of a PageHeader.

Files to Review

Does this example address your development requirements/objectives?

(you will be redirected to DevExpress.com to submit your response)

Example Code

MainWindow.xaml
XAML
<dxc:DXWindow x:Class="PrintDocumentFromUIWpf.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:dxc="http://schemas.devexpress.com/winfx/2008/xaml/core" xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors" xmlns:dxp="http://schemas.devexpress.com/winfx/2008/xaml/printing" Title="MainWindow" dxc:ThemeManager.ThemeName="Azure" Height="330" Width="280" Loaded="DXWindow_Loaded"> <dxc:DXWindow.Resources> <BooleanToVisibilityConverter x:Key="booleanToVisibilityConverter" /> <DataTemplate x:Key="detailTemplate"> <dxe:TextEdit EditValue="{Binding Path=Content}" IsPrintingMode="True" Width="100" /> </DataTemplate> <DataTemplate x:Key="reportHeaderTemplate"> <dxe:TextEdit EditValue="{Binding Path=Content.EditValue}" FontSize="20.25pt" Width="584" HorizontalContentAlignment="Center" IsPrintingMode="True" /> </DataTemplate> <DataTemplate x:Key="reportFooterTemplate"> <dxe:TextEdit Text="{Binding Path=Content.EditValue}" FontSize="20.25pt" Width="584" HorizontalContentAlignment="Center" IsPrintingMode="True" /> </DataTemplate> <DataTemplate x:Key="pageHeaderTemplate"> <dxe:TextEdit Text="{Binding Path=Content.EditValue}" FontSize="15.25pt" Width="400" IsPrintingMode="True" /> </DataTemplate> <DataTemplate x:Key="pageFooterTemplate"> <StackPanel> <dxe:TextEdit Text="{Binding Path=Content.Title}" FontSize="15.25pt" Width="500" IsPrintingMode="True" /> <dxe:TextEdit Visibility="{Binding Path=Content.PrintPageInfo, Converter={StaticResource booleanToVisibilityConverter}}" dxp:ExportSettings.TargetType="PageNumber" dxp:PageNumberExportSettings.Kind="NumberOfTotal" dxp:PageNumberExportSettings.Format="Page {0} of {1}" IsPrintingMode="True" /> </StackPanel> </DataTemplate> </dxc:DXWindow.Resources> <StackPanel> <GroupBox Header="Document Elements"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="100" /> <ColumnDefinition Width="*" /> <ColumnDefinition Width="100" /> </Grid.ColumnDefinitions> <Label Margin="5">Report Header:</Label> <dxe:TextEdit Name="teReportHeader" Grid.Column="2" Margin="5" /> <Label Grid.Row="1" Margin="5">Page Header:</Label> <dxe:TextEdit Name="tePageHeader" Grid.Column="2" Grid.Row="1" Margin="5" /> <Label Grid.Row="2" Margin="5">Detail Content:</Label> <dxe:TextEdit Name="teDetail" Grid.Column="2" Grid.Row="2" Margin="5" /> <Label Grid.Row="3" Margin="5">Page Footer:</Label> <dxe:TextEdit Name="tePageFooter" Grid.Column="2" Grid.Row="3" Margin="5" /> <Label Grid.Row="4" Margin="5">Report Footer:</Label> <dxe:TextEdit Name="teReportFooter" Grid.Column="2" Grid.Row="4" Margin="5" /> <Label Grid.Row="5" Margin="5">Detail Count:</Label> <dxe:SpinEdit Name="seDetail" Grid.Column="2" Grid.Row="5" Margin="5" MinValue="0" IsFloatValue="False" IsTextEditable="False" /> <Label Grid.Row="6" Margin="5">Print Page Info:</Label> <dxe:CheckEdit Name="cePageInfo" Grid.Column="2" Grid.Row="6" Margin="5" HorizontalAlignment="Center" /> </Grid> </GroupBox> <Button Width="100" Margin="5" Content="Print Preview" Click="btnPrint_Click" /> </StackPanel> </dxc:DXWindow>
MainWindow.xaml.cs(vb)
C#
using System; using System.Windows; using DevExpress.Xpf.Core; using DevExpress.Xpf.Printing; namespace PrintDocumentFromUIWpf { public partial class MainWindow : DXWindow { public MainWindow() { InitializeComponent(); } void DXWindow_Loaded(object sender, RoutedEventArgs e) { InitDefaultVlaues(); } void btnPrint_Click(object sender, RoutedEventArgs e) { CreateAndPreviewLink(); } void InitDefaultVlaues() { teReportHeader.EditValue = "Report Header"; tePageHeader.EditValue = "Page Header"; teDetail.EditValue = "Detail"; tePageFooter.EditValue = "Page Footer"; teReportFooter.EditValue = "Report Footer"; seDetail.EditValue = 400; cePageInfo.EditValue = true; } void CreateAndPreviewLink() { var link = new SimpleLink { ReportHeaderTemplate = (DataTemplate)Resources["reportHeaderTemplate"], PageHeaderTemplate = (DataTemplate)Resources["pageHeaderTemplate"], DetailTemplate = (DataTemplate)Resources["detailTemplate"], PageFooterTemplate = (DataTemplate)Resources["pageFooterTemplate"], ReportFooterTemplate = (DataTemplate)Resources["reportFooterTemplate"], ReportHeaderData = teReportHeader, PageHeaderData = tePageHeader, PageFooterData = new PageFooterDataContext { Title = tePageFooter.EditValue.ToString(), PrintPageInfo = (bool)cePageInfo.IsChecked }, ReportFooterData = teReportFooter, DetailCount = (int)seDetail.EditValue }; link.CreateDetail += link_CreateDetail; link.ShowPrintPreviewDialog(this); } void link_CreateDetail(object sender, CreateAreaEventArgs e) { e.Data = string.Format("{0} (Row {1})", teDetail.EditValue, e.DetailIndex); } } public class PageFooterDataContext { public string Title { get; set; } public bool PrintPageInfo { get; set; } } }

Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.