Conditional Styling

This topic will demonstrate how the cells of RadVirtualGrid can be styled conditionally.

This is achieved through the usage of the following three events in conjunction.

  • OverlayBrushesNeeded: The event arguments expose a Brushes collection which can be populated with the Brushes that will be conditionally applied further. When they are preliminarily known, the respective Geometry can be easily drawn at once, instead of applying a style for each cell individually. Thus, the rendering performance of the control is kept unaffected.

Example 1: Adding Brushes through the OverlayBrushesNeeded event

private void VirtualGrid_OverlayBrushesNeeded(object sender,  
        Telerik.Windows.Controls.VirtualGrid.OverlayBrushesEventArgs e) 
    { 
        e.Brushes.Add(Brushes.Red); 
        e.Brushes.Add(Brushes.Yellow); 
    } 
  • CellDecorationsNeeded: This is the event through which the actual styling of the cells can be applied. The event arguments expose the following properties:

    • Background: Its value must be set to be one of the Brushes provided by the Brushes collection populated through the OverlayBrushesNeeded event.
    • ColumnIndex: The column the given cell corresponds to.
    • RowIndex: The row the given cell corresponds to.
    • FontFamily: Sets the FontFamily of the cell.
    • FontSize: Sets the FontSize of the cell.
    • Foreground: Sets the Brush for the Foreground of the cell.
    • CellTextAlignment: Sets the cell's text alignment.

Applying conditional styling through the CellDecorationsNeeded event

private void VirtualGrid_CellDecorationsNeeded(object sender,  
        Telerik.Windows.Controls.VirtualGrid.CellDecorationEventArgs e) 
    { 
        if (e.ColumnIndex % 2 == 0) 
        { 
            e.Background = Brushes.Red; 
        } 
        else 
        { 
            e.Background = Brushes.Yellow; 
            e.Foreground = Brushes.Red; 
            e.CellTextAlignment = TextAlignment.Right; 
        } 
    } 

For the effects of the CellTextAlignment property to be applied, the control's MeasureTextOnRender property has to be set to True.

Figure 1: RadVirtualGrid with conditional styling applied

RadVirtualGrid with conditional styling applied

  • HeaderCellDecorationsNeeded: This event was exposed in the R3 2019 version of the control. It allows for the styling of the header cells. Its event arguments are the same as the ones for the CellDecorationsNeeded event.

See also

In this article
Not finding the help you need? Improve this article