Cell Style and Template in WPF Pivot Grid

15 Jul 202113 minutes to read

Cell styles

Column, row, summary, and value cells of a grid can be formatted independently by using the specific style properties as listed below:

  • ColumnHeaderCellStyle: Specifies the style for column headers.
  • RowHeaderCellStyle: Specifies the style for row headers.
  • SummaryCellStyle: Specifies the style for summary cells.
  • ValueCellStyle: Specifies the style for value cells.

The following properties of the cell can be customized.

Property Name</th><th>
Gets or sets the background color of a grid cell.</td><td>
Gets or sets the font family of a grid cell.</td><td>
Gets or sets the font size of a grid cell.</td><td>
Gets or sets the font weight of a grid cell.</td><td>
Gets or sets the foreground color of a grid cell.</td><td>

Defining the cell styles in pivot grid

After defining the pivot grid control, raise the loaded event of pivot grid control. Inside the PivotGrid_Loaded() event, set the properties of cell styles of the pivot grid control.

Refer to the following code snippet.

  • C#
  • public partial class MainWindow: Window {
        public MainWindow() {
            pivotGrid += pivotGrid_Loaded;
        void pivotGrid_Loaded(object sender, RoutedEventArgs e) {
            // Specifying the Background color for Grid column header
            pivotGrid.ColumnHeaderCellStyle.Background = new SolidColorBrush(Color.FromRgb(175, 209, 255));
            // Specifying the Background color for Grid row header
            pivotGrid.RowHeaderCellStyle.Background = new SolidColorBrush(Color.FromRgb(175, 209, 255));
            // Specifying the Background color for Grid summary cell
            pivotGrid.SummaryCellStyle.Background = new SolidColorBrush(Color.FromRgb(206, 225, 248));
            // Specifying the Font Family for Grid column header
            pivotGrid.ColumnHeaderCellStyle.FontFamily = new FontFamily("Arial");
            // Specifying the Font Family for Grid row header
            pivotGrid.RowHeaderCellStyle.FontFamily = new FontFamily("Arial");
            // Specifying the Font Family for Grid summary cell
            pivotGrid.SummaryCellStyle.FontFamily = new FontFamily("Calibri");

    PivotGrid shows customized styles

    Cell templates

    Cell templates feature of pivot grid allows you to define the templates to change the appearance of elements, such as column, row, summary, and value cells that are present in the grid. The style for each element in the grid should be defined of PivotGridTemplateCell type. The customized template can be defined for the following properties of pivot grid:

    • ColumnHeaderCellStyle
    • RowHeaderCellStyle
    • SummaryHeaderStyle
    • SummaryCellStyle
    • ValueCellStyle

    The expander’s in the grid can also be customized with any UIElement and it should be named as “PART_Expander” to perform drill-up and drill-down operations.

    Defining cell templates in pivot grid

    After defining the pivot grid control, define your own style for row, column, value, and summary cells and assign that style to the corresponding property in the pivot grid control.

    Here, the row header cells are defined with our own style by overriding an expander icon and cell’s TextBlock. After defining the style, it is applied to RowHeaderCellStyle of the pivot grid control.

    Refer to the following code snippet.

  • XAML
  • <Window.Resources>
                <ObjectDataProvider x:Key="data" ObjectType="{x:Type local:ProductSales}" MethodName="GetSalesData"/>
             <Style x:Key="rowStyle" TargetType="{x:Type syncfusion:PivotGridTemplateCell}">
                    <Setter Property="MinHeight" Value="25"/>
                    <Setter Property="Template">
                            <ControlTemplate TargetType="{x:Type syncfusion:PivotGridTemplateCell}">
                                <StackPanel  Grid.Column="1" Orientation="Horizontal" Background="#FF119EDA">
                                    <!--Expander control style, to enable expand/collapse control name should be "PART_Expander" -->
                                    <Expander Margin="1" x:Name="PART_Expander" IsExpanded="{Binding IsExpanded,RelativeSource={RelativeSource TemplatedParent}}" Visibility="{Binding Converter={StaticResource expanderVisiblityConverter}}" Grid.Column="0" />
                                    <!--Image block-->
                                    <Image Margin="2,4,1,0" Grid.Column="1" VerticalAlignment="Top" HorizontalAlignment="Center">
                                            <Style TargetType="{x:Type Image}">
                                                    <DataTrigger Binding="{Binding Path=Text, RelativeSource={RelativeSource TemplatedParent}}" Value="Car">
                                                        <Setter Property="Source" Value="{StaticResource Car}"/>
                                                        <Setter Property="Width" Value="32"/>
                                                        <Setter Property="Height" Value="32"/>
                                                    <DataTrigger Binding="{Binding Path=Text, RelativeSource={RelativeSource TemplatedParent}}" Value="Bike">
                                                        <Setter Property="Source" Value="{StaticResource Bike}"/>
                                                        <Setter Property="Width" Value="32"/>
                                                        <Setter Property="Height" Value="32"/>
                                    <TextBlock Grid.Column="1" Margin="3,4,2,0"
                                               Text="{Binding Path=Text, RelativeSource={RelativeSource TemplatedParent}}"
                                               VerticalAlignment="Top" FontFamily="Segoe UI" FontSize="12"
            <syncfusion:PivotGridControl HorizontalAlignment="Left" Name="pivotGrid" VerticalAlignment="Top" VisualStyle="Metro" ItemSource="{Binding   Source={StaticResource data}}">
                    <syncfusion:PivotItem FieldHeader="Product" FieldMappingName="Product" TotalHeader="Total" />
                    <syncfusion:PivotItem FieldHeader="Date" FieldMappingName="Date" TotalHeader="Total" />
                    <syncfusion:PivotItem FieldHeader="Country" FieldMappingName="Country" TotalHeader="Total" />
                    <syncfusion:PivotItem FieldHeader="State" FieldMappingName="State" TotalHeader="Total" />
                    <syncfusion:PivotComputationInfo CalculationName="Total" FieldName="Amount" Format="C" SummaryType="DoubleTotalSum" />
                    <syncfusion:PivotComputationInfo CalculationName="Total" FieldName="Quantity" SummaryType="Count" />
                    <syncfusion:PivotGridCellStyle Style="{StaticResource rowStyle}" />

    PivotGrid shows templated cells


    You can refer to our WPF Pivot Grid feature tour page for its groundbreaking feature representations. You can also explore our WPF Pivot Grid example to knows how to organizes and summarizes business data and displays the result in a cross-table format.