How to: Use Custom Printing Templates

  • 6 minutes to read

This example demonstrates how to use grid's printing templates to change the grid printing appearance.

To learn more on how to implement similar functionality in Silverlight, refer to the T246198 example.

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:dx="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"
                    xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid"
                    >

    <DataTemplate x:Key="CustomPrintHeaderTemplate">
        <dxe:TextEdit Text="Car rental list" IsPrintingMode="True" Background="#333E5E" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Padding="12" 
                      Width="{Binding Path=Content.(dxg:GridPrintingHelper.PrintRowInfo).TotalHeaderWidth}"
                      Foreground="#FFFFFFFF"  FontWeight="Bold" FontSize="20" HorizontalContentAlignment="Right"/>
    </DataTemplate>

    <dx:BoolToObjectConverter x:Key="BoolToTextConverter" TrueValue="Avaliable" FalseValue="NotAvaliable"/>   

    <DataTemplate x:Key="CustomPrintRowTemplate">
        <ContentControl Name="contentControl" Foreground="{Binding Foreground, RelativeSource={RelativeSource TemplatedParent}}">

            <Grid DataContext="{Binding DataContext.Content, RelativeSource={RelativeSource TemplatedParent}}"
                  Width="{Binding Path=(dxg:GridPrintingHelper.PrintRowInfo).TotalHeaderWidth}" >
                <Grid DataContext="{Binding DataContext.Content.Row, RelativeSource={RelativeSource TemplatedParent}}"                        
                    Margin="0,5,0,0"
                    >
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="*"/>
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="150"/>
                    </Grid.ColumnDefinitions>

                    <dxe:ImageEdit Source="{Binding Path=Image}"
                                IsPrintingMode="True" 

                                HorizontalAlignment="Stretch"
                                Grid.Row="0" Grid.RowSpan="2" Grid.ColumnSpan="2"
                                />

                    <dxe:TextEdit EditValue="{Binding Path=PlainText}" IsPrintingMode="True" FontSize="32" FontWeight="Bold" 
                                Foreground="White" Background="#7D7D7D7D" VerticalAlignment="Top" HorizontalAlignment="Stretch" HorizontalContentAlignment="Right" Padding="15,2" Grid.ColumnSpan="2"
                                />

                    <StackPanel dxp:ExportSettings.TargetType="Panel" 
                            dxp:ExportSettings.Background="#7D7D7D7D"
                            Grid.Row="1" Grid.Column="1" Orientation="Vertical" 
                            >

                        <dxe:TextEdit Text="Description:" IsPrintingMode="True" Foreground="White" FontWeight="Medium" Padding="2"  Margin="1,0" dxp:ExportSettings.Background="#7D7D7D7D"/>
                        <dxe:TextEdit EditValue="{Binding MemoText}" 
                                      TextWrapping="Wrap" IsPrintingMode="True" Foreground="White" Padding="2" Margin="1,0" dxp:ExportSettings.Background="#7D7D7D7D"
                                      />
                        <dxe:TextEdit Text="State:" IsPrintingMode="True" Foreground="White" FontWeight="Medium" Padding="2" Margin="1,7,1,0" dxp:ExportSettings.Background="#7D7D7D7D"/>
                        <dxe:TextEdit Text="{Binding BooleanMember, Converter={StaticResource BoolToTextConverter}}" 
                                      IsPrintingMode="True" Foreground="White" Padding="2" Margin="1,0" dxp:ExportSettings.Background="#7D7D7D7D"
                                      />
                        <dxe:TextEdit Text="Price:" IsPrintingMode="True" Foreground="White" FontWeight="Medium" Padding="2" Margin="1,7,1,0" dxp:ExportSettings.Background="#7D7D7D7D"/>
                        <dxe:TextEdit EditValue="{Binding Price}"
                                      DisplayFormatString=" {0:c2}"
                                      IsPrintingMode="True" Foreground="White" Padding="2" Margin="1,0" dxp:ExportSettings.Background="#7D7D7D7D"
                                      />
                    </StackPanel>
                </Grid>
            </Grid>
        </ContentControl>
    </DataTemplate>

    <DataTemplate x:Key="CustomPrintFooterTemplate">
        <dxe:TextEdit IsPrintingMode="True" Background="#333E5E" HorizontalAlignment="Right"  VerticalAlignment="Stretch" Padding="12" 
                      Width="{Binding Path=Content.(dxg:GridPrintingHelper.PrintRowInfo).TotalHeaderWidth}"
                      Foreground="#FFFFFFFF"  FontWeight="Bold" FontSize="12" HorizontalContentAlignment="Center"
                      Text="{Binding Path=Content.View.DataControl.Columns[PlainText].TotalSummaryText, Mode=OneWay}"
                      >
        </dxe:TextEdit>
    </DataTemplate>

</ResourceDictionary>