Back to Devexpress

How to: Custom draw cells

windowsforms-5652-controls-and-libraries-tree-list-examples-appearance-how-to-custom-draw-cells.md

latest2.3 KB
Original Source

How to: Custom draw cells

  • Nov 13, 2018

The following sample code handles the TreeList.CustomDrawNodeCell event. It is used to perform custom painting of node cells. All node cells are painted in the same manner.

The image below illustrates the result of executing the sample code.

csharp
using System.Drawing;
using System.Drawing.Drawing2D;
using DevExpress.XtraTreeList;

private void treeList1_CustomDrawNodeCell(object sender, CustomDrawNodeCellEventArgs e) {
    // Create brushes for cells.
    Brush backBrush, foreBrush;
    if (e.Node != (sender as TreeList).FocusedNode)
    {
        backBrush = e.Cache.GetGradientBrush(e.Bounds, Color.PapayaWhip, Color.PeachPuff, LinearGradientMode.ForwardDiagonal);
        foreBrush = Brushes.Black;
    }
    else
    {
        backBrush = Brushes.DarkBlue;
        foreBrush = e.Cache.GetSolidBrush(Color.PeachPuff);
    }
    // Fill the background.
    e.Cache.FillRectangle(backBrush, e.Bounds);
    // Paint the node value.
    e.Cache.DrawString(e.CellText, e.Appearance.Font, foreBrush, e.Bounds,
      e.Appearance.GetStringFormat());

    // Prohibit default painting.
    e.Handled = true;
}
vb
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports DevExpress.XtraTreeList

Private Sub TreeList1_CustomDrawNodeCell(sender As Object, e As DevExpress.XtraTreeList.CustomDrawNodeCellEventArgs) Handles TreeList1.CustomDrawNodeCell
    ' Create brushes for cells.
    Dim BackBrush, ForeBrush As Brush
    If e.Node.Id <> sender.FocusedNode.Id Then
        BackBrush = e.Cache.GetGradientBrush(e.Bounds, Color.PapayaWhip,
                                             Color.PeachPuff, LinearGradientMode.ForwardDiagonal)
        ForeBrush = Brushes.Black
    Else
        BackBrush = Brushes.DarkBlue
        ForeBrush = Brushes.PeachPuff
    End If
    ' Fill the background.
    e.Cache.FillRectangle(BackBrush, e.Bounds)
    ' Paint the node value.
    e.Cache.DrawString(e.CellText, e.Appearance.Font, ForeBrush, e.Bounds,
                       e.Appearance.GetStringFormat())

    ' Prohibit default painting
    e.Handled = True
End Sub