Back to Devexpress

PivotGridField.CellTemplate Property

wpf-devexpress-dot-xpf-dot-pivotgrid-dot-pivotgridfield-b334d39d.md

latest11.3 KB
Original Source

PivotGridField.CellTemplate Property

Gets or sets a template used to display cells that correspond to the current field. This is a dependency property.

Namespace : DevExpress.Xpf.PivotGrid

Assembly : DevExpress.Xpf.PivotGrid.v25.2.dll

NuGet Package : DevExpress.Wpf.PivotGrid

Declaration

csharp
public DataTemplate CellTemplate { get; set; }
vb
Public Property CellTemplate As DataTemplate

Property Value

TypeDescription
DataTemplate

A DataTemplate object used to display cells that correspond to the current field.

|

Remarks

If the CellTemplate is not specified, the cells are displayed via the PivotGridControl.FieldCellTemplate template (or the PivotGridControl.FieldCellKpiTemplate if this field displays KPI graphics). The PivotGridField.ActualCellTemplate property allows you to obtain which template is currently used.

View Example: Pivot Grid - Customize CellTemplate with Different Colors

You can implement custom logic to choose the required template using the PivotGridField.CellTemplateSelector property.

Example

The PivotGrid control does not support the data editing functionality out of the box because it displays aggregated data. This example demonstrates how to implement a custom CellTemplate with the in-place editing functionality.

This example implements the base editing features. It does not allow going to the next cell by pressing the tab or arrow keys.

The PivotGridEditHelper class implements the in-place cell editor. When editing is finished, the editor calls the pvotGridControl1_OnCellEdit method. This method retrieves the underlying data for the edited cell and adjusts them so that their sum equals the new value entered in the cell.

View Example: How to Edit a Cell with the Cell Editing Template

xaml
<dx:ThemedWindow
    x:Class="HowToEditCell.MainWindow"
    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:dxpg="http://schemas.devexpress.com/winfx/2008/xaml/pivotgrid"
    xmlns:local="clr-namespace:HowToEditCell"
    Width="800"
    Height="450"
    Loaded="Window_Loaded"
    Title="How to Edit Cell">

    <Grid>
        <dxpg:PivotGridControl
            x:Name="pivotGridControl1"
            local:PivotGridEditHelper.OnCellEdit="pivotGridControl1_OnCellEdit"
            local:PivotGridEditHelper.UseHelper="{Binding RelativeSource={RelativeSource Self}}">
            <dxpg:PivotGridControl.Fields>
                <dxpg:PivotGridField
                    x:Name="fieldSales"
                    Area="DataArea"
                    Caption="Product Sales"
                    FieldName="ExtendedPrice">
                    <dxpg:PivotGridField.CellTemplate>
                        <DataTemplate>
                            <dxe:TextEdit
                                x:Name="edit"
                                HorizontalContentAlignment="Right"
                                DisplayFormatString="c2"
                                EditMode="InplaceInactive"
                                EditValue="{Binding Value, Mode=OneWay}"
                                local:PivotGridEditHelper.LostFocusCommand="{x:Static local:PivotGridEditHelper.Enter}"
                                Mask="[0-9]*\.[0-9]{0,2}"
                                MaskType="RegEx">
                                <dxe:TextEdit.InputBindings>
                                    <KeyBinding
                                        Key="Enter"
                                        Command="{x:Static local:PivotGridEditHelper.Enter}"
                                        CommandParameter="{Binding ElementName=edit}" />
                                    <MouseBinding
                                        Command="{x:Static local:PivotGridEditHelper.StartEdit}"
                                        CommandParameter="{Binding ElementName=edit}"
                                        MouseAction="LeftClick" />
                                </dxe:TextEdit.InputBindings>
                            </dxe:TextEdit>
                        </DataTemplate>
                    </dxpg:PivotGridField.CellTemplate>
                </dxpg:PivotGridField>
                <dxpg:PivotGridField
                    x:Name="fieldYear"
                    AllowFilter="False"
                    Area="ColumnArea"
                    Caption="Year"
                    FieldName="OrderDate"
                    GroupInterval="DateYear" />
                <dxpg:PivotGridField
                    x:Name="fieldCategoryName"
                    Area="RowArea"
                    AreaIndex="0"
                    Caption="Category"
                    FieldName="CategoryName" />
                <dxpg:PivotGridField
                    x:Name="fieldProductName"
                    Area="RowArea"
                    AreaIndex="1"
                    Caption="Product"
                    FieldName="ProductName" />
                <dxpg:PivotGridField
                    Area="DataArea"
                    Caption="Percent Of Column"
                    CellFormat="p"
                    FieldName="ExtendedPrice"
                    Name="fieldExtendedPrice2"
                    SummaryDisplayType="PercentOfColumn" />
            </dxpg:PivotGridControl.Fields>
        </dxpg:PivotGridControl>
    </Grid>

</dx:ThemedWindow>
csharp
using System;
using System.Collections.ObjectModel;
using System.Windows;
using DevExpress.Xpf.Core;
using DevExpress.Xpf.PivotGrid;

namespace HowToEditCell {
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : ThemedWindow {

        public ObservableCollection<MyOrderRow> OrderSourceList { get; set; }

        public MainWindow() {
            InitializeComponent();
        }

        void Window_Loaded(object sender, RoutedEventArgs e) {
            OrderSourceList = DatabaseHelper.CreateData();
            pivotGridControl1.DataSource = OrderSourceList;
            pivotGridControl1.BestFit();
        }

        void pivotGridControl1_OnCellEdit(DependencyObject sender, PivotCellEditEventArgs args) {
            PivotGridControl pivotGrid = (PivotGridControl)sender;
            PivotGridField fieldExtendedPrice = pivotGrid.Fields["ExtendedPrice"];
            PivotDrillDownDataSource ds = args.CreateDrillDownDataSource();
            decimal difference = args.NewValue - args.OldValue;
            decimal factor = (difference == args.NewValue) ? (difference / ds.RowCount) : (difference / args.OldValue);
            for(int i = 0; i < ds.RowCount; i++) {
                decimal value = Convert.ToDecimal(ds[i][fieldExtendedPrice]);
                decimal newValue = (value == 0m) ? factor : value * (1m + factor);
                ds.SetValue(i, fieldExtendedPrice, newValue);
            }
        }
    }
}
vb
Imports System
Imports System.Collections.ObjectModel
Imports System.Windows
Imports DevExpress.Xpf.Core
Imports DevExpress.Xpf.PivotGrid

Namespace HowToEditCell
    ''' <summary>
    ''' Interaction logic for MainWindow.xaml
    ''' </summary>
    Partial Public Class MainWindow
        Inherits ThemedWindow

        Public Property OrderSourceList() As ObservableCollection(Of MyOrderRow)

        Public Sub New()
            InitializeComponent()
        End Sub

        Private Sub Window_Loaded(ByVal sender As Object, ByVal e As RoutedEventArgs)
            OrderSourceList = DatabaseHelper.CreateData()
            pivotGridControl1.DataSource = OrderSourceList
            pivotGridControl1.BestFit()
        End Sub

        Private Sub pivotGridControl1_OnCellEdit(ByVal sender As DependencyObject, ByVal args As PivotCellEditEventArgs)
            Dim pivotGrid As PivotGridControl = CType(sender, PivotGridControl)
            Dim fieldExtendedPrice As PivotGridField = pivotGrid.Fields("ExtendedPrice")
            Dim ds As PivotDrillDownDataSource = args.CreateDrillDownDataSource()
            Dim difference As Decimal = args.NewValue - args.OldValue
            Dim factor As Decimal = If(difference = args.NewValue, (difference / ds.RowCount), (difference / args.OldValue))
            For i As Integer = 0 To ds.RowCount - 1
                Dim value As Decimal = Convert.ToDecimal(ds(i)(fieldExtendedPrice))
                Dim newValue As Decimal = If(value = 0D, factor, value * (1D + factor))
                ds.SetValue(i, fieldExtendedPrice, newValue)
            Next i
        End Sub
    End Class
End Namespace

The following code snippet (auto-collected from DevExpress Examples) contains a reference to the CellTemplate property.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

wpf-pivot-grid-define-custom-cell-template-to-performing-data-editing/CS/HowToEditCell/MainWindow.xaml#L28

xml
</dxpg:PivotGridField.DataBinding>
<dxpg:PivotGridField.CellTemplate>
    <DataTemplate>

See Also

FieldCellTemplate

ActualCellTemplate

FieldCellTemplateSelector

Cell

PivotGridField Class

PivotGridField Members

DevExpress.Xpf.PivotGrid Namespace