Back to Devexpress

TreeList.CustomDrawColumnHeader Event

windowsforms-devexpress-dot-xtratreelist-dot-treelist-f4c5c781.md

latest13.2 KB
Original Source

TreeList.CustomDrawColumnHeader Event

Provides the ability to custom paint column headers and the column button.

Namespace : DevExpress.XtraTreeList

Assembly : DevExpress.XtraTreeList.v25.2.dll

NuGet Packages : DevExpress.Win.Navigation, DevExpress.Win.TreeList

Declaration

csharp
[DXCategory("CustomDraw")]
public event CustomDrawColumnHeaderEventHandler CustomDrawColumnHeader
vb
<DXCategory("CustomDraw")>
Public Event CustomDrawColumnHeader As CustomDrawColumnHeaderEventHandler

Event Data

The CustomDrawColumnHeader event's data class is CustomDrawColumnHeaderEventArgs. The following properties provide information specific to this event:

PropertyDescription
AppearanceGets the painted element’s appearance settings. Inherited from CustomDrawEventArgs.
BoundsGets the painted element’s bounding rectangle. Inherited from CustomDrawEventArgs.
CacheGets an object specifying the storage for the most used pens, fonts and brushes. Inherited from CustomDrawEventArgs.
CaptionGets or sets the text displayed within the painted column header.
CaptionRectGets the rectangle where the column caption is intended to be drawn.
ColumnGets the column whose header is painted.
ColumnInfoGets an object containing information about the painted element.
ColumnTypeGets a value indicating the type of element to be painted.
GraphicsGets an object used to paint. Inherited from CustomDrawEventArgs.
HandledGets or sets a value specifying whether an event was handled and that the default element painting is therefore not required. Inherited from CustomDrawEventArgs.
HotTrackGets a value indicating whether the painted column header is hot tracked.
IsRightToLeftGets a value indicating whether the TreeList’s elements are aligned to support locales using right-to-left fonts. Inherited from CustomDrawEventArgs.
ObjectArgsGets an object containing information about the painted element. Inherited from CustomDrawEventArgs.
PainterGets the painter object that provides the default element’s painting mechanism. Inherited from CustomDrawEventArgs.
PressedGets a value indicating whether the painted element is pressed.
SortShapeRectGets the rectangle where the image indicating sort order is intended to be drawn.

The event data class exposes the following methods:

MethodDescription
DefaultDraw()Performs default painting of an element. Inherited from CustomDrawEventArgs.
DrawHtml(HtmlTemplate, DxHtmlPainterContext, Action<DxHtmlPainterArgs>)Paints the required HTML template inside an element that raised this event. The context parameter allows you to assign an object that transfers mouse events to template elements. Inherited from CustomDrawEventArgs.
DrawHtml(HtmlTemplate, Action<DxHtmlPainterArgs>)Paints the required HTML template inside an element that raised this event. Inherited from CustomDrawEventArgs.

Remarks

The event’s parameters provide you all the information needed to paint either a column header or the column button. Use the event’s CustomDrawColumnHeaderEventArgs.ColumnType parameter to determine the kind of element to be drawn. Note that column headers are visible if the TreeListOptionsView.ShowColumns option is enabled. The column button is visible if both the TreeListOptionsView.ShowColumns and TreeListOptionsView.ShowIndicator options are active.

See the Custom Draw Scenarios topic for information on using custom draw events.

Important

Do not change cell values, modify the control’s layout, or change the control’s object model in the events used for custom control painting. Actions that update the layout can cause the control to malfunction.

Example

The following sample code shows how to handle the TreeList.CustomDrawColumnHeader event to paint the focused column’s caption using a bold font. Note that no manual painting is performed in the TreeList.CustomDrawColumnHeader event handler. The event is used to modify the appearance of painted column headers.

In the example, the TreeList.FocusedColumnChanged event is handled to force column header repainting on navigating through columns (by default, column headers are not repainted on column navigation).

The image below displays the result of the sample code execution. The focused column is painted with a bold caption.

csharp
using DevExpress.XtraTreeList;
using DevExpress.Utils;

private void treeList1_CustomDrawColumnHeader(object sender, DevExpress.XtraTreeList.CustomDrawColumnHeaderEventArgs e) {
    if (e.ColumnType == HitInfoType.ColumnButton || e.ColumnType == HitInfoType.BehindColumn) return;
    if (e.Column == (sender as TreeList).FocusedColumn)
        e.Appearance.FontStyleDelta = FontStyle.Bold;
    else
        e.Appearance.FontStyleDelta = FontStyle.Regular;
}

private void treeList1_FocusedColumnChanged(object sender, FocusedColumnChangedEventArgs e) {
    TreeList tl = sender as TreeList;
    tl.InvalidateColumnHeader(e.Column);
    tl.InvalidateColumnHeader(e.OldColumn);
}
vb
Imports DevExpress.XtraTreeList
Imports DevExpress.Utils

Private Sub TreeList1_CustomDrawColumnHeader(ByVal sender As Object, _
ByVal e As CustomDrawColumnHeaderEventArgs) Handles TreeList1.CustomDrawColumnHeader
   If e.ColumnType = HitInfoType.ColumnButton Or e.ColumnType = HitInfoType.BehindColumn Then Exit Sub
   Dim TL As TreeList = sender
   If e.Column Is TL.FocusedColumn Then
      e.Appearance.FontStyleDelta = FontStyle.Bold
   Else
      e.Appearance.FontStyleDelta = FontStyle.Regular
   End If
End Sub

Private Sub TreeList1_FocusedColumnChanged(ByVal sender As Object, _
ByVal e As FocusedColumnChangedEventArgs) Handles TreeList1.FocusedColumnChanged
   Dim TL As TreeList = sender
   TL.InvalidateColumnHeader(e.Column)
   TL.InvalidateColumnHeader(e.OldColumn)
End Sub

Example

The following sample code handles the TreeList.CustomDrawColumnHeader event to perform custom painting of column headers. Column headers are drawn differently for the normal, pressed and hot tracked states. The CustomDrawColumnHeaderEventArgs.Pressed and CustomDrawColumnHeaderEventArgs.HotTrack properties are used to identify the column header’s current state.

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

private void treeList1_CustomDrawColumnHeader(object sender, 
CustomDrawColumnHeaderEventArgs e) {
   // column button custom painting is not performed
   if (e.ColumnType == HitInfoType.ColumnButton) return;

   Brush backBrush;
   // determining the brush used to fill column headers background regerding to their state
   if (e.Pressed) 
      backBrush = e.Cache.GetGradientBrush(e.Bounds, Color.Gray, Color.Silver, 
        LinearGradientMode.Vertical);
   else if (e.HotTrack)
      backBrush = e.Cache.GetGradientBrush(e.Bounds, Color.Silver, Color.Gray, 
         LinearGradientMode.Vertical);
   else 
      backBrush = e.Cache.GetGradientBrush(e.Bounds, Color.DeepSkyBlue, Color.Blue, 
        LinearGradientMode.Vertical);
   // filling the background
   e.Cache.FillRectangle(backBrush, e.Bounds);
   // painting borders
   e.Cache.DrawRectangle(e.Cache.GetPen(Color.LightGray), e.Bounds);
   // painting column headers captions
     e.Cache.DrawString(e.Caption, e.Appearance.Font, e.Cache.GetSolidBrush(Color.White),
       e.CaptionRect, e.Appearance.GetStringFormat());

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

Private Sub TreeList1_CustomDrawColumnHeader(ByVal sender As Object, _
ByVal e As CustomDrawColumnHeaderEventArgs) _
Handles TreeList1.CustomDrawColumnHeader
    ' column button custom painting is not performed
    If e.ColumnType = HitInfoType.ColumnButton Then Exit Sub

    Dim BackBrush As Brush
    ' determining the brush used to fill column headers background regerding to their state
    If e.Pressed Then
        BackBrush = e.Cache.GetGradientBrush(e.Bounds, Color.Gray, Color.Silver,
    LinearGradientMode.Vertical)
    ElseIf (e.HotTrack) Then
        BackBrush = e.Cache.GetGradientBrush(e.Bounds, Color.Silver, Color.Gray,
    LinearGradientMode.Vertical)
    Else
        BackBrush = e.Cache.GetGradientBrush(e.Bounds, Color.DeepSkyBlue, Color.Blue,
    LinearGradientMode.Vertical)
    End If
    ' filling the background
    e.Cache.FillRectangle(BackBrush, e.Bounds)
    ' painting borders
    e.Cache.DrawRectangle(e.Cache.GetPen(Color.LightGray), e.Bounds)
    ' painting column headers captions
    e.Cache.DrawString(e.Caption, e.Appearance.Font, e.Cache.GetSolidBrush(Color.White),
                       e.CaptionRect, e.Appearance.GetStringFormat())

    ' prohibiting default painting
    e.Handled = True
End Sub

See Also

CustomDrawNodeIndicator

CustomDrawRowFooter

CustomDrawRowFooterCell

CustomDrawFooter

CustomDrawFooterCell

TreeList Class

TreeList Members

DevExpress.XtraTreeList Namespace