Our What's New in v24.2 webpage includes product-specific surveys. Your response to our survey questions will help us measure product satisfaction for features released in this major update and help us refine our plans for our next major release.
How to: Select Cell Templates Based on Custom Logic
3 minutes to read
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.
usingSystem;
usingSystem.Globalization;
usingSystem.Windows;
usingSystem.Windows.Controls;
usingSystem.Windows.Data;
usingSystem.Windows.Markup;
using DevExpress.Xpf.PivotGrid.Internal;
namespaceDXPivotGrid_SelectingCellTemplate {
publicpartialclassMainWindow : Window {
publicMainWindow() {
InitializeComponent();
picotGridControl1.DataSource =
new nwindDataSetTableAdapters.SalesPersonTableAdapter().GetData();
}
}
publicclassCellTemplateSelector : DataTemplateSelector {
publicoverride 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;
elsereturn mainWindow.FindResource("NormalCellTemplate") as DataTemplate;
}
}
publicclassRoundConverter : MarkupExtension, IValueConverter {
#region IValueConverter MemberspublicobjectConvert(objectvalue, Type targetType,
object parameter, CultureInfo culture) {
return System.Convert.ToInt32(value);
}
publicobjectConvertBack(objectvalue, Type targetType,
object parameter, CultureInfo culture) {
thrownew NotImplementedException();
}
#endregionpublicoverrideobjectProvideValue(IServiceProvider serviceProvider) {
returnthis;
}
}
}
ImportsMicrosoft.VisualBasicImportsSystemImportsSystem.GlobalizationImportsSystem.WindowsImportsSystem.Windows.ControlsImportsSystem.Windows.DataImportsSystem.Windows.MarkupImports DevExpress.Xpf.PivotGrid.Internal
Namespace DXPivotGrid_SelectingCellTemplate
PartialPublicClass MainWindow
Inherits Window
PublicSubNew()
InitializeComponent()
picotGridControl1.DataSource = _
New nwindDataSetTableAdapters.SalesPersonTableAdapter().GetData()
EndSubEndClassPublicClass CellTemplateSelector
Inherits DataTemplateSelector
PublicOverridesFunction SelectTemplate(ByVal item AsObject, _
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 AsDouble = Convert.ToDouble(cell.Value) / Convert.ToDouble(cell.RowTotalValue)
' Applies the Default template to the Row Grand Total cells.If cell.RowValue IsNothingThenReturnTryCast(mainWindow.FindResource("DefaultCellTemplate"), DataTemplate)
EndIf' 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.7OrElse share < 0.3ThenReturnTryCast(mainWindow.FindResource("HighlightedCellTemplate"), DataTemplate)
ElseReturnTryCast(mainWindow.FindResource("NormalCellTemplate"), DataTemplate)
EndIfEndFunctionEndClassPublicClass RoundConverter
Inherits MarkupExtension
Implements IValueConverter
#Region "IValueConverter Members"PublicFunction Convert(ByVal value AsObject, ByVal targetType As Type, _
ByVal parameter AsObject, ByVal culture As CultureInfo) AsObject _
Implements IValueConverter.Convert
Return System.Convert.ToInt32(value)
EndFunctionPublicFunction ConvertBack(ByVal value AsObject, ByVal targetType As Type, _
ByVal parameter AsObject, ByVal culture As CultureInfo) AsObject _
Implements IValueConverter.ConvertBack
ThrowNew NotImplementedException()
EndFunction#EndRegionPublicOverridesFunction ProvideValue(ByVal serviceProvider As IServiceProvider) AsObjectReturnMeEndFunctionEndClassEndNamespace
Was this page helpful?
Thanks for your feedback!
How can we improve this help topic?
Additional comments/thoughts:
If you have any questions, submit a ticket to our Support Center.