Templating Headers and Cells
RadPivotGrid provides few options to template its cells and headers out of the box. You can either apply one template to all of them using the CellTemplate, RowHeaderTemplate and ColumnHeaderTemplate properties of RadPivotGrid. Or you can use the CellTemplateSelector, RowHeaderTemplateSelector and ColumnHeaderTemplateSelector properties in order to implement custom TemplateSelector and template the headers and cells per condition. This article will show you how to improve the RadPivotGrid appearance by applying custom Templates and implementing custom TemplateSelectors.
Using Custom Cell and Header Templates
The CellTemplate, RowHeaderTemplate and ColumnHeaderTemplate properties of RadPivotGrid will help you to easily apply a custom template to all of the cells or to all of the column and row headers. You would simply need to define the needed DataTemplates and apply them to RadPivotGrid. For example if you need to have green cells and headers with Italic FontStyle you will need to define the following templates:
And apply them as shown below:
Figure 1 demonstrates the final result.
Figure 1: Custom cell and header templates.
Using Custom CellTemplateSelector
Implementing a custom CellTemplateSelector allows you to apply different templates per a condition. For example depending on the cell value you can change its Background in order to indicate lower or higher value than a certain one. To get it started you would need to create a new class inheriting from the DataTemplateSelector class and define the two Templates - one for the lower values and one for the higher ones. Afterwards you have to override the SelectTemplate and implement the needed custom logic. So finally the custom CellTemplateSelector should look the following way:
Next thing to do is to define the required templates in the XAML as shown below:
And the last step would be to assign the CellTemplateSelector to RadPivotGrid:
You can see the final result on Figure 2.
Figure 2: Cells with values below 1000 are colored in red and the other cells in green using CellTemplateSelector.
Find a runnable project of the previous example in the WPF Samples GitHub repository.
Using Custom HeaderTemplateSelectors
By implementing a custom HeaderTemplateSelector you are able to modify the templates only of the column header cells or row header cells. In this section you will see how to add images in the different header cells. Firstly you will need to create custom HeaderTemplateSelector that inherits from DataTemplateSelector and define a DataTemplate which will be used for the Product header cells. The selector should look as shown below:
Next thing to do is to define the required templates in the XAML the following way:
Using an IValueConverter you will be able to return the path for the needed image depending content of the header cell:
Finaly you will have to assing the HeaderTemplateSelector to the RowHeaderTemplateSelector and ColumnHeaderTemplateSelector properties of RadPivotGrid.
You can see the final result on Figure 3.
Figure 3: Header cells with different images.
Find a runnable project of the previous example in the WPF Samples GitHub repository.