Category / Section
How to give conditional formatting for a pivot value cell by comparing with other value cells
1 min read
Usually our PivotGrid doesn’t support on conditional formatting with unbound pivot fields (Pivot Rows, Pivot Columns and Pivot Calculations). However the above requirement can be meet with Cell Templates and GetRawItems features which are support in our PivotGrid control.
XAML
<Grid.Resources> <local:BackgroundConverter x:Key="backConverter" /> <Style x:Key="valueCellStyle" TargetType="{x:Type syncfusion:PivotGridTemplateCell}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type syncfusion:PivotGridTemplateCell}"> <StackPanel Background="{Binding Converter={StaticResource backConverter}, ElementName=pivotGrid1}"> <TextBlock Grid.Column="1" Text="{Binding Path=Text, RelativeSource={RelativeSource TemplatedParent}}" TextWrapping="Wrap" HorizontalAlignment="Center" VerticalAlignment="Center" FontFamily="Segoe UI" FontSize="12"/> </StackPanel> </ControlTemplate> </Setter.Value> </Setter> </Style> </Grid.Resources>
C#
class BackgroundConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { if (value is PivotGridControl) { PivotGridControl pivotGrid = value as PivotGridControl; if (MainWindow.CurrentCellRowIndex == 0 && MainWindow.CurrentCellColumnIndex == 0) { MainWindow.CurrentCellRowIndex = pivotGrid.PivotColumns.Count + ((pivotGrid.PivotCalculations.Count > 1) ? 1 : 0); MainWindow.CurrentCellColumnIndex = pivotGrid.PivotRows.Count; } var rawItems = pivotGrid.EditManager.GetRawItemsFor(MainWindow.CurrentCellRowIndex, MainWindow.CurrentCellColumnIndex); if (rawItems.Count > 0) { if (rawItems.Any(i => i is ProductSales && (i as ProductSales).Product == "Bike")) { ////Show yellow color on background if any one record has product property as "Bike" return Brushes.Yellow; } return Brushes.LightBlue; //// else light blue color. } } return null; } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } }
Figure: Customized pivot Grid with conditional formats