Skip to main content
A newer version of this page is available. .

How to: Custom paint column headers, summary footer, footer cells and indicator cells

  • 3 minutes to read

The following example custom paints column headers, summary footer, footer cells and indicator cells via the TreeList.CustomDrawColumnHeader, TreeList.CustomDrawFooter, TreeList.CustomDrawFooterCell and TreeList.CustomDrawNodeIndicator events.

Three functions are declared that implement custom painting. These are used to draw the background of the raised and sunken elements, and to paint the text with the predefined alignment within elements.

The image below shows the result.

CustomDraw - TBH1

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

// Custom paints the column headers.
private void treeList1_CustomDrawColumnHeader(object sender, DevExpress.XtraTreeList.CustomDrawColumnHeaderEventArgs e) {
    if (e.Pressed)
        paintSunkenBackground(e);
    else
        paintRaisedBackground(e);
    if (e.ColumnType == HitInfoType.Column)
        paintHAlignedText(e, e.Column.GetCaption(), StringAlignment.Near);
    e.Handled = true;
}

// Custom paints the summary footer.
private void treeList1_CustomDrawFooter(object sender, DevExpress.XtraTreeList.CustomDrawEventArgs e) {
    paintRaisedBackground(e);
    e.Handled = true;
}

// Custom paints the cells of the summary footer.
private void treeList1_CustomDrawFooterCell(object sender, DevExpress.XtraTreeList.CustomDrawFooterCellEventArgs e) {
    if (e.Text == String.Empty) return;
    paintSunkenBackground(e);
    paintHAlignedText(e, e.Text, StringAlignment.Far);
    e.Handled = true;
}

// Custom paints the indicator cells.
private void treeList1_CustomDrawNodeIndicator(object sender, DevExpress.XtraTreeList.CustomDrawNodeIndicatorEventArgs e) {
    paintRaisedBackground(e);
    e.Handled = true;
}

// Custom draw methods:

// Paints the background of raised elements.
private void paintRaisedBackground(CustomDrawEventArgs e)
{
    Brush brush = e.Cache.GetGradientBrush(e.Bounds, Color.Yellow, Color.Gold, LinearGradientMode.Vertical);
    e.Cache.FillRectangle(brush, e.Bounds);
    e.Cache.DrawRectangle(e.Cache.GetPen(Color.LightGray), e.Bounds);
}

// Paints the background of sunken elements.
private void paintSunkenBackground(CustomDrawEventArgs e)
{
    Brush brush = e.Cache.GetGradientBrush(e.Bounds, Color.CornflowerBlue, Color.LightGray, LinearGradientMode.Vertical);
    e.Cache.FillRectangle(brush, e.Bounds);
    e.Cache.DrawRectangle(e.Cache.GetPen(Color.LightGray), e.Bounds);
}

// Paints the aligned text.
private void paintHAlignedText(CustomDrawEventArgs e, String text, StringAlignment align)
{
    Brush textBrush = e.Cache.GetSolidBrush(Color.Black);

    StringFormat outStringFormat = e.Appearance.GetStringFormat();
    outStringFormat.Alignment = align;
    outStringFormat.LineAlignment = StringAlignment.Center;
    outStringFormat.FormatFlags = StringFormatFlags.NoWrap;
    outStringFormat.Trimming = StringTrimming.EllipsisCharacter;

    e.Cache.DrawString(text, e.Appearance.Font, textBrush, e.Bounds, outStringFormat);
}