Version

Customizing Cells with Templates – Procedure and Code Example (xamPivotGrid)

Topic Overview

Purpose

This topic is a step-by-step walkthrough on how to customize xamPivotGrid™ cells by applying custom DataTemplates for their content.

Required background

The following table lists the topics required as a prerequisite to understanding this topic.

Topic Purpose

This section serves as an introduction to the xamPivotGrid control’s key features and functionalities. The topics listed here will give you a better idea of why you would want to use xamPivotGrid in your applications.

This topic demonstrates how to get started with the xamPivotGrid control by providing step-by-step procedure for adding this control to a WPF application.

The xamPivotGrid control enables you to specify custom data templates for data cells and for header column and row cells. This topic is an overview of this feature.

Customizing Cells with Templates

Introduction

The procedure below will help you understand how to use the cell customization feature of the xamPivotGrid.

Preview

The following screenshot is a preview of the final result.

xamPivotGrid Customizing Cells with Templates Procedure and Code Example 1.png

Requirements

To complete the procedure, you need a WPF application where the xamPivotGrid control is used. In this example the Sales Data Sample data source is used, but you can modify the code examples to work with other data.

Overview

This topic takes you step-by-step toward creating and applying custom templates for xamPivotGrid data and header cells. The following is a conceptual overview of the process:

Steps

  1. Create a set of DataTemplates.

    First you need to create DataTemplates that you will later assign to the cell content. When these templates are used in the xamPivotGrid, the DataContext for data cells is a string containing the value that should be displayed in the cell. For header cells the data context is the Tuple for the respective row/column.

    Therefore to get the value of the cell in the DataTemplate you need to do a simple data context binding: {Binding}

    Add the data templates to the Resources section of your page or to another ResourceDictionary that is accessible from your page.

  1. Apply templates for data cells.

    1. Create a DataCellTemplate and add it to the xamPivotGrid’s DataCellTemplates collection.

      In order to use a template from the ones created in step 1, you need add a DataCellTemplate object to the DataCellTemplates collection and set its Template property to the DataTemplate you want to apply.

      You can specify as many DataCellTemplates as you wish.

    2. Specify which cells the template should apply to.

      This is achieved by setting values to the remaining properties of the DataCellTemplates.

      1. Specify column and row hierarchy.

        When you set the ColumnHierarchy property, the data template will be applied to all data cells that are in the specified column hierarchy. The value should be the same as the Caption property of the Hierarchy that you want to target. Analogically you can specify a value for the RowHierarchy property. This will apply the template to the cells that are in both the column and row hierarchies.

      2. Specify column and row level.

        Setting values to these properties enables you to create even more precise criteria defining where the data template will be applied. Setting the Level to an integer number will cause the template to be applied only to cells at that level. Level numbering starts at 1 (not at 0).

      3. Specify column and row label.

        Setting these properties will apply the template only to cells whose column/row header has the specified label. This enables you to apply custom DataTemplate to a specific cell.

      Note
      Note:

      All of these properties are optional, although at least one of them should be set so that the template is applied. If some/all of them are set, the template will be applied to cells that match the intersection of the properties’ values. If there is a conflict between two or more templates, the most specific one will be applied. If there is no matched cell, the template will not be applied at all.

  1. Apply templates for row and column headers.

    1. Create a HeaderTemplate and add it to the ColumnHeaderTemplates/ RowHeaderTemplates collection.

      This is done analogically to the way the DataCellTemplates are added to the DataCellTemplates collection of the xamPivotGrid.

    2. Specify the column/row header cells which the template will be applied to.

      1. Specify the hierarchy.

        Use the Hierarchy property to assign the template to all column/row header cells.

      2. Specify the level.

        Setting the Level property applies the template to cells at the specified level. As with DataCellTemplate, level numbering starts at 1.

      3. Specify the label.

        This way the template is applied only to header cells whose column/row Caption is the same as the one you specified.

Code Example: Customizing Cells with Templates

Description

The example below shows you how to create and apply custom DataTemplates to xamPivotGrid data and header cells.

Code

Add DataTemplates to the Resources of the page.

In XAML:

<UserControl.Resources>
    <DataTemplate x:Key="DataTemplate1">
        <TextBlock Foreground="Red" Text="{Binding}" />
    </DataTemplate>
    <DataTemplate x:Key="DataTemplate2">
        <TextBlock Foreground="Green" Text="{Binding}" />
    </DataTemplate>
    <DataTemplate x:Key="DataTemplate3">
        <Grid Background="LightGreen">
            <TextBlock Text="{Binding}" />
        </Grid>
    </DataTemplate>
    <DataTemplate x:Key="DataTemplate4">
        <Grid Background="LightBlue">
            <TextBlock Text="{Binding}" />
        </Grid>
    </DataTemplate>
</UserControl.Resources>

Apply the DataTemplates.

In XAML:

<ig:XamPivotGrid
    x:Name="pivotGrid"
    DataSource="{StaticResource FlatDataSource}">
    <ig:XamPivotGrid.DataCellTemplates>
        <ig:DataCellTemplate
            Template="{StaticResource DataTemplate1}"
            ColumnHierarchy="Product"
            ColumnLabel="Clothing" />
        <ig:DataCellTemplate
            Template="{StaticResource DataTemplate2}"
            RowHierarchy="Seller"
            RowLevel="2"
            RowLabel="John Smith" />
    </ig:XamPivotGrid.DataCellTemplates>
    <ig:XamPivotGrid.ColumnHeaderTemplates>
        <ig:HeaderTemplate
            Template="{StaticResource DataTemplate3}"
            Label="Bikes" />
    </ig:XamPivotGrid.ColumnHeaderTemplates>
    <ig:XamPivotGrid.RowHeaderTemplates>
        <ig:HeaderTemplate
            Template="{StaticResource DataTemplate4}"
            Hierarchy="Seller"
            Level="2" />
    </ig:XamPivotGrid.RowHeaderTemplates>
</ig:XamPivotGrid>

In C#:

…
pivotGrid.DataCellTemplates.Add((DataCellTemplate)this.Resources["DataTemplate1"]);
pivotGrid.DataCellTemplates.Add((DataCellTemplate)this.Resources["DataTemplate2"]);
pivotGrid.ColumnHeaderTemplates.Add((HeaderTemplate)this.Resources["DataTemplate3"]);
pivotGrid.RowHeaderTemplates.Add((HeaderTemplate)this.Resources["DataTemplate4"]);
…

In Visual Basic:

…
pivotGrid.DataCellTemplates.Add(DirectCast(Me.Resources("DataTemplate1"), DataCellTemplate))
pivotGrid.DataCellTemplates.Add(DirectCast(Me.Resources("DataTemplate2"), DataCellTemplate))
pivotGrid.ColumnHeaderTemplates.Add(DirectCast(Me.Resources("DataTemplate3"), HeaderTemplate))
pivotGrid.RowHeaderTemplates.Add(DirectCast(Me.Resources("DataTemplate4"), HeaderTemplate))
…

Related Topics

The following topics provide additional information related to this topic.

Topic Purpose

This is a list of the most notable properties related to the Cell customization feature of the xamPivotGrid.

Explanation on how to use the CellControlAttached event to apply custom styles to data cells based on the cell’s value.