Category / Section
How to give alternative row background color in PivotGrid
1 min read
XAML
<Style x:Key="valStyle" TargetType="{x:Type syncfusion:PivotGridTemplateCell}"> <Setter Property="MinHeight" Value="25"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type syncfusion:PivotGridTemplateCell}"> <StackPanel Grid.Column="1" Orientation="Horizontal" Background="{Binding ElementName=pivotGrid1, Converter={StaticResource backcolor}, NotifyOnTargetUpdated=True}"> <!--Image block--> <Image Margin="2,4,1,0" Grid.Column="1" Source="{Binding ElementName=pivotGrid1, Converter={StaticResource imgConverter}, NotifyOnTargetUpdated=True}" TargetUpdated="Image_TargetUpdated" VerticalAlignment="Top" HorizontalAlignment="Center"> <Image.Style> <Style TargetType="{x:Type Image}"> </Style> </Image.Style> </Image> <TextBlock Grid.Column="1" Margin="3,4,2,0" Text="{Binding Path=Text, RelativeSource={RelativeSource TemplatedParent}}" TextWrapping="Wrap" VerticalAlignment="Top" FontFamily="Segoe UI" FontSize="12" TargetUpdated="Image_TargetUpdated" /> </StackPanel> </ControlTemplate> </Setter.Value> </Setter> </Style>
C#
class BackColorConverter : IValueConverter { Brush brush = Brushes.White; public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { Syncfusion.Windows.Controls.PivotGrid.PivotGridControl pivotGrid = value as Syncfusion.Windows.Controls.PivotGrid.PivotGridControl; int rowIndex = MainWindow.rowIdex; int colIndex = MainWindow.colIndex; if (rowIndex % 2 == 0) { brush = Brushes.Pink; return brush; } else { if (MainWindow.colIndex == -1 && MainWindow.rowIdex == -1) { brush = Brushes.Pink; } else brush = Brushes.Red; } return brush; } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } }
Figure: Pivot Grid shows alternate row backgrounds