PivotGridField.PrintCellTemplateSelector Property
Gets or sets a template selector that chooses a print template for data cells based on custom logic. This is a dependency property.
Namespace: DevExpress.Xpf.PivotGrid
Assembly:
DevExpress.Xpf.PivotGrid.v24.1.dll
NuGet Package:
DevExpress.Wpf.PivotGrid
Declaration
public DataTemplateSelector PrintCellTemplateSelector { get; set; }
Public Property PrintCellTemplateSelector As DataTemplateSelector
Property Value
A template that defines the presentation of data cells when the pivot grid is printed is specified by the PivotGridField.PrintCellTemplate property. If you have more than one template that can be used to render cells, you can implement custom logic to choose the required template. To do this, derive from the DataTemplateSelector class, implement the SelectTemplate method that returns a template which meets the required condition, and assign an instance of this class to the PrintCellTemplateSelector property.
The PrintCellTemplateSelector is in effect only if the PivotGridField.PrintCellTemplate property is set to null (Nothing in Visual Basic). Otherwise, the PivotGridField.PrintCellTemplate template is used.
To learn more, see Printing and Exporting.
Example
This example demonstrates how to select print templates for the DXPivotGrid elements based on a custom logic.
The logic for selecting the data cell print template is based on the ratio of the data cell value to the grand total value. If the ratio is between 0.2 and 0.8, the template selector applies a template that highlights the cell text and adds the warning sign to the cell.
using System;
using System.Windows;
using System.Windows.Controls;
using DevExpress.Xpf.PivotGrid;
using DevExpress.Xpf.Printing;
namespace DXPivotGrid_SelectingPrintTemplate {
public partial class MainWindow : Window {
public MainWindow() {
InitializeComponent();
picotGridControl1.DataSource =
new nwindDataSetTableAdapters.SalesPersonTableAdapter().GetData();
}
private void btnPrint_Click(object sender, RoutedEventArgs e) {
PrintHelper.ShowPrintPreview(this, picotGridControl1);
}
}
public class CellTemplateSelector : DataTemplateSelector {
public override DataTemplate SelectTemplate(object item, DependencyObject container) {
Window mainWindow = Application.Current.MainWindow;
CellElementData cell = (CellElementData)item;
// Calculate the ratio of the cell value to the grand total.
double share = Convert.ToDouble(cell.Value) / Convert.ToDouble(cell.ColumnTotalValue);
// Apply the Normal template to the Column Grand Total cells.
if (cell.ColumnValue == null)
return mainWindow.FindResource("NormalCellTemplate") as DataTemplate;
// If the ratio is far from 0.5, use the Highlighted template.
// Otherwise, use the Normal template.
if (share > 0.8 || share < 0.2)
return mainWindow.FindResource("HighlightedCellTemplate") as DataTemplate;
else
return mainWindow.FindResource("NormalCellTemplate") as DataTemplate;
}
}
}
<Window xmlns:dxpg="http://schemas.devexpress.com/winfx/2008/xaml/pivotgrid"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:local="clr-namespace:DXPivotGrid_SelectingPrintTemplate"
x:Class="DXPivotGrid_SelectingPrintTemplate.MainWindow"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:dxp="http://schemas.devexpress.com/winfx/2008/xaml/printing"
xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors"
Title="MainWindow" Height="765" Width="485">
<Window.Resources>
<DataTemplate x:Key="NormalCellTemplate">
<dxe:TextEdit Text="{Binding Path=Value, Mode=OneWay}"
IsPrintingMode="True" Padding="4,2"
dxp:TextExportSettings.Text="{Binding DisplayText, Mode=OneWay}"
dxp:TextExportSettings.TextValue="{Binding Value, Mode=OneWay}"
dxp:TextExportSettings.TextValueFormatString="{Binding ValueFormat, Mode=OneWay}"
dxp:TextExportSettings.XlsExportNativeFormat="{Binding UseNativeFormat, Mode=OneWay}"
VerticalContentAlignment="Center"
HorizontalContentAlignment="Right"
BorderThickness="{Binding Border, Mode=OneWay}"
BorderBrush="#808080"
/>
</DataTemplate>
<DataTemplate x:Key="HighlightedCellTemplate">
<Border dxp:ExportSettings.TargetType="Panel" Padding="4,2"
dxp:ExportSettings.BorderThickness="{Binding Border, Mode=OneWay}"
dxp:ExportSettings.BorderColor="#808080" >
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Image Margin="0,0,10,0" Source="Warning.png"
HorizontalAlignment="Right"
dxp:ExportSettings.TargetType="Image"
Grid.Column="0" />
<dxe:TextEdit Text="{Binding Path=Value, Mode=OneWay}"
IsPrintingMode="True"
dxp:TextExportSettings.Text="{Binding DisplayText, Mode=OneWay}"
dxp:TextExportSettings.TextValue="{Binding Value, Mode=OneWay}"
dxp:TextExportSettings.TextValueFormatString="{Binding ValueFormat, Mode=OneWay}"
dxp:TextExportSettings.XlsExportNativeFormat="{Binding UseNativeFormat, Mode=OneWay}"
VerticalContentAlignment="Center"
HorizontalContentAlignment="Right"
FontStyle="Oblique"
Grid.Column="1" Foreground="Red" />
</Grid>
</Border>
</DataTemplate>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<dxpg:PivotGridControl x:Name="picotGridControl1" PrintFieldCellTemplate="{x:Null}">
<dxpg:PivotGridControl.Fields>
<dxpg:PivotGridField Name="fieldCountry" FieldName="Country" Area="ColumnArea" />
<dxpg:PivotGridField Name="fieldYear" FieldName="OrderDate" Area="RowArea"
Caption="Year" GroupInterval="DateYear" />
<dxpg:PivotGridField Name="fieldMonth" FieldName="OrderDate" Area="RowArea"
Caption="Month" GroupInterval="DateMonth" />
<dxpg:PivotGridField Name="fieldQuantity" FieldName="Quantity" Area="DataArea" />
</dxpg:PivotGridControl.Fields>
<dxpg:PivotGridControl.PrintFieldCellTemplateSelector>
<local:CellTemplateSelector />
</dxpg:PivotGridControl.PrintFieldCellTemplateSelector>
</dxpg:PivotGridControl>
<Button x:Name="btnPrint" Grid.Row="1" Content="Print Preview"
Margin="5" Height="25" Width="200"
Click="btnPrint_Click" />
</Grid>
</Window>
Imports System
Imports System.Windows
Imports System.Windows.Controls
Imports DevExpress.Xpf.PivotGrid
Imports DevExpress.Xpf.Printing
Namespace DXPivotGrid_SelectingPrintTemplate
Partial Public Class MainWindow
Inherits Window
Public Sub New()
InitializeComponent()
picotGridControl1.DataSource = (New nwindDataSetTableAdapters.SalesPersonTableAdapter()).GetData()
End Sub
Private Sub btnPrint_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
PrintHelper.ShowPrintPreview(Me, picotGridControl1)
End Sub
End Class
Public Class CellTemplateSelector
Inherits DataTemplateSelector
Public Overrides Function SelectTemplate(ByVal item As Object, ByVal container As DependencyObject) As DataTemplate
'INSTANT VB NOTE: The variable mainWindow was renamed since it may cause conflicts with calls to static members of the user-defined type with this name:
Dim mainWindow_Renamed As Window = Application.Current.MainWindow
Dim cell As CellElementData = DirectCast(item, CellElementData)
' Calculates the share of a cell value in the Column Grand Total value.
Dim share As Double = Convert.ToDouble(cell.Value) / Convert.ToDouble(cell.ColumnTotalValue)
' Applies the Normal template to the Column Grand Total cells.
If cell.ColumnValue Is Nothing Then
Return TryCast(mainWindow_Renamed.FindResource("NormalCellTemplate"), DataTemplate)
End If
' If the share is too far from 50%, the Highlighted template is selected.
' Otherwise, the Normal template is applied to the cell.
If share > 0.8 OrElse share < 0.2 Then
Return TryCast(mainWindow_Renamed.FindResource("HighlightedCellTemplate"), DataTemplate)
Else
Return TryCast(mainWindow_Renamed.FindResource("NormalCellTemplate"), DataTemplate)
End If
End Function
End Class
End Namespace
See Also