TreeList.CustomDrawColumnHeader Event
Provides the ability to custom paint column headers and the column button.
Namespace: DevExpress.XtraTreeList
Assembly: DevExpress.XtraTreeList.v24.2.dll
Declaration
[DXCategory("CustomDraw")]
public event CustomDrawColumnHeaderEventHandler CustomDrawColumnHeader
Event Data
The CustomDrawColumnHeader event's data class is CustomDrawColumnHeaderEventArgs. The following properties provide information specific to this event:
Property | Description |
---|---|
Appearance | Gets the painted element’s appearance settings. Inherited from CustomDrawEventArgs. |
Bounds | Gets the painted element’s bounding rectangle. Inherited from CustomDrawEventArgs. |
Cache | Gets an object specifying the storage for the most used pens, fonts and brushes. Inherited from CustomDrawEventArgs. |
Caption | Gets or sets the text displayed within the painted column header. |
CaptionRect | Gets the rectangle where the column caption is intended to be drawn. |
Column | Gets the column whose header is painted. |
ColumnInfo | Gets an object containing information about the painted element. |
ColumnType | Gets a value indicating the type of element to be painted. |
Graphics | Gets an object used to paint. Inherited from CustomDrawEventArgs. |
Handled | Gets or sets a value specifying whether an event was handled and that the default element painting is therefore not required. Inherited from CustomDrawEventArgs. |
HotTrack | Gets a value indicating whether the painted column header is hot tracked. |
IsRightToLeft | Gets a value indicating whether the TreeList’s elements are aligned to support locales using right-to-left fonts. Inherited from CustomDrawEventArgs. |
ObjectArgs | Gets an object containing information about the painted element. Inherited from CustomDrawEventArgs. |
Painter | Gets the painter object that provides the default element’s painting mechanism. Inherited from CustomDrawEventArgs. |
Pressed | Gets a value indicating whether the painted element is pressed. |
SortShapeRect | Gets the rectangle where the image indicating sort order is intended to be drawn. |
The event data class exposes the following methods:
Method | Description |
---|---|
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.
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);
}
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.
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;
}