Back to Devexpress

PivotGridControl.FieldCellTemplateSelector Property

wpf-devexpress-dot-xpf-dot-pivotgrid-dot-pivotgridcontrol-ea146f50.md

latest11.4 KB
Original Source

PivotGridControl.FieldCellTemplateSelector Property

Gets or sets an object that chooses a cell template based on custom logic. This is a dependency property.

Namespace : DevExpress.Xpf.PivotGrid

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

NuGet Package : DevExpress.Wpf.PivotGrid

Declaration

csharp
public DataTemplateSelector FieldCellTemplateSelector { get; set; }
vb
Public Property FieldCellTemplateSelector As DataTemplateSelector

Property Value

TypeDescription
DataTemplateSelector

A DataTemplateSelector descendant that chooses a template based on custom logic.

|

Remarks

A template that defines the presentation of data cells is specified by the PivotGridControl.FieldCellTemplate 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 FieldCellTemplateSelector property.

The FieldCellTemplateSelector is used for those fields whose PivotGridField.CellTemplateSelector property is not specified.

The FieldCellTemplateSelector is in effect only if the PivotGridControl.FieldCellTemplate property is set to null ( Nothing in Visual Basic). Otherwise, the PivotGridControl.FieldCellTemplate template is used.

If a field displays KPI graphics, the PivotGridControl.FieldCellKpiTemplate and PivotGridControl.FieldCellKpiTemplateSelector properties are used for this field.

Example

The following example demonstrates how to select the cell template based on custom logic.In this example, data cell values are represented by progress bars. The template used to display the data cells is selected based on the share of the data cell value in the Row Grand Total value. If this share is bigger than 80% or less than 20%, a red progress bar is displayed in the cell. Otherwise, a blue bar is displayed.

xaml
<Window x:Class="DXPivotGrid_SelectingCellTemplate.MainWindow"
        xmlns:dxpg="http://schemas.devexpress.com/winfx/2008/xaml/pivotgrid"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:core="http://schemas.devexpress.com/winfx/2008/xaml/core"
        xmlns:local="clr-namespace:DXPivotGrid_SelectingCellTemplate"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="230" Width="725">
    <Window.Resources>
        <DataTemplate x:Key="NormalCellTemplate">
            <ProgressBar Foreground="Blue" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"  
             Maximum="{Binding Path=RowTotalValue, Mode=OneWay, Converter={local:RoundConverter}}"
             Value="{Binding Path=Value, Mode=OneWay, Converter={local:RoundConverter}}"
             core:ThemeManager.ThemeName="Office2007Silver" Minimum="0" Margin="3"/>
        </DataTemplate>
        <DataTemplate x:Key="HighlightedCellTemplate">
            <ProgressBar Foreground="Red" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"  
             Maximum="{Binding Path=RowTotalValue, Mode=OneWay, Converter={local:RoundConverter}}"
             Value="{Binding Path=Value, Mode=OneWay, Converter={local:RoundConverter}}"
             core:ThemeManager.ThemeName="Office2007Silver" Minimum="0" Margin="3"/>
        </DataTemplate>
        <DataTemplate x:Key="DefaultCellTemplate">
            <TextBlock Text="{Binding Path=Value}" HorizontalAlignment="Right"
                       VerticalAlignment="Center" Margin="5"/>
        </DataTemplate>
    </Window.Resources>
    <Grid>
        <dxpg:PivotGridControl x:Name="picotGridControl1" FieldCellTemplate="{x:Null}">
            <dxpg:PivotGridControl.Fields>
                <dxpg:PivotGridField Name="fieldCountry" FieldName="Country" Area="RowArea"/>
                <dxpg:PivotGridField Name="fieldYear" FieldName="OrderDate" Area="ColumnArea"
                                     Caption="Year" GroupInterval="DateYear"/>
                <dxpg:PivotGridField Name="fieldMonth" FieldName="OrderDate" Area="ColumnArea"
                                     Caption="Month" GroupInterval="DateMonth"/>
                <dxpg:PivotGridField Name="fieldQuantity" FieldName="Quantity" Area="DataArea"/>
            </dxpg:PivotGridControl.Fields>
            <dxpg:PivotGridControl.FieldCellTemplateSelector>
                <local:CellTemplateSelector/>
            </dxpg:PivotGridControl.FieldCellTemplateSelector>
        </dxpg:PivotGridControl>
    </Grid>
</Window>
csharp
using System;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Markup;
using DevExpress.Xpf.PivotGrid.Internal;

namespace DXPivotGrid_SelectingCellTemplate {
    public partial class MainWindow : Window {
        public MainWindow() {
            InitializeComponent();
            picotGridControl1.DataSource =
                new nwindDataSetTableAdapters.SalesPersonTableAdapter().GetData();
        }
    }
    public class CellTemplateSelector : DataTemplateSelector {
        public override DataTemplate SelectTemplate(object item, DependencyObject container) {
            Window mainWindow = Application.Current.MainWindow;
            CellsAreaItem cell = (CellsAreaItem)item;

            // Calculates the share of a cell value in the Row Grand Total value.
            double share = Convert.ToDouble(cell.Value) / Convert.ToDouble(cell.RowTotalValue);

            // Applies the Default template to the Row Grand Total cells.
            if (cell.RowValue == null)
                return mainWindow.FindResource("DefaultCellTemplate") as DataTemplate;

            // 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.7 || share < 0.3)
                return mainWindow.FindResource("HighlightedCellTemplate") as DataTemplate;
            else
                return mainWindow.FindResource("NormalCellTemplate") as DataTemplate;
        }
    }
    public class RoundConverter : MarkupExtension, IValueConverter {
        #region IValueConverter Members
        public object Convert(object value, Type targetType,
                              object parameter, CultureInfo culture) {
            return System.Convert.ToInt32(value);
        }
        public object ConvertBack(object value, Type targetType,
                                  object parameter, CultureInfo culture) {
            throw new NotImplementedException();
        }
        #endregion
        public override object ProvideValue(IServiceProvider serviceProvider) {
            return this;
        }
    }
}
vb
Imports Microsoft.VisualBasic
Imports System
Imports System.Globalization
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Data
Imports System.Windows.Markup
Imports DevExpress.Xpf.PivotGrid.Internal

Namespace DXPivotGrid_SelectingCellTemplate
    Partial Public Class MainWindow
        Inherits Window
        Public Sub New()
            InitializeComponent()
            picotGridControl1.DataSource = _
                New nwindDataSetTableAdapters.SalesPersonTableAdapter().GetData()
        End Sub
    End Class
    Public Class CellTemplateSelector
        Inherits DataTemplateSelector
        Public Overrides Function SelectTemplate(ByVal item As Object, _
                ByVal container As DependencyObject) As DataTemplate
            Dim mainWindow As Window = Application.Current.MainWindow
            Dim cell As CellsAreaItem = CType(item, CellsAreaItem)

            ' Calculates the share of a cell value in the Row Grand Total value.
            Dim share As Double = Convert.ToDouble(cell.Value) / Convert.ToDouble(cell.RowTotalValue)

            ' Applies the Default template to the Row Grand Total cells.
            If cell.RowValue Is Nothing Then
                Return TryCast(mainWindow.FindResource("DefaultCellTemplate"), 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.7 OrElse share < 0.3 Then
                Return TryCast(mainWindow.FindResource("HighlightedCellTemplate"), DataTemplate)
            Else
                Return TryCast(mainWindow.FindResource("NormalCellTemplate"), DataTemplate)
            End If
        End Function
    End Class
    Public Class RoundConverter
        Inherits MarkupExtension
        Implements IValueConverter
        #Region "IValueConverter Members"
        Public Function Convert(ByVal value As Object, ByVal targetType As Type, _
                ByVal parameter As Object, ByVal culture As CultureInfo) As Object _
                Implements IValueConverter.Convert
            Return System.Convert.ToInt32(value)
        End Function
        Public Function ConvertBack(ByVal value As Object, ByVal targetType As Type, _
                ByVal parameter As Object, ByVal culture As CultureInfo) As Object _
                Implements IValueConverter.ConvertBack
            Throw New NotImplementedException()
        End Function
        #End Region
        Public Overrides Function ProvideValue(ByVal serviceProvider As IServiceProvider) As Object
            Return Me
        End Function
    End Class
End Namespace

See Also

FieldCellTemplate

CellTemplateSelector

CellTemplate

Cell

PivotGridControl Class

PivotGridControl Members

DevExpress.Xpf.PivotGrid Namespace