Back to Devexpress

TreeList.CustomDrawNodeButton Event

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

latest9.2 KB
Original Source

TreeList.CustomDrawNodeButton Event

Gives you the ability to perform custom painting of expand buttons.

Namespace : DevExpress.XtraTreeList

Assembly : DevExpress.XtraTreeList.v25.2.dll

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

Declaration

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

Event Data

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

PropertyDescription
AllowDrawBackgroundGets or sets whether to draw a background that detaches the button from the tree hierarchy lines.
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.
ExpandedGets a value indicating whether the painted button’s corresponding node is expanded.
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.
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.

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

Expand buttons are used to expand/collapse nodes which have child nodes. Handle the CustomDrawNodeButton event to custom paint expand buttons.

The event’s parameters provide all the information needed to perform node button painting. The CustomDrawNodeButtonEventArgs.Expanded property allows you to determine whether the painted button corresponds to the expanded or collapsed state of a node.

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 handles the TreeList.CustomDrawNodeButton event. It is used to perform custom painting of buttons used to expand/collapse nodes.

The image below displays the result of sample code execution.

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

private void treeList1_CustomDrawNodeButton(object sender, CustomDrawNodeButtonEventArgs e) {
    Rectangle rect = Rectangle.Inflate(e.Bounds, 0, -2);

    // painting background
    Brush backBrush = e.Cache.GetGradientBrush(rect, Color.Blue, Color.LightSkyBlue,
      LinearGradientMode.ForwardDiagonal);

    e.Cache.FillRectangle(backBrush, rect);
    // painting borders
    e.Cache.DrawRectangle(e.Cache.GetPen(Color.LightGray), rect);

    // determining the character to display
    string displayCharacter = e.Expanded ? "-" : "+";
    // formatting the output character
    StringFormat outCharacterFormat = e.Appearance.GetStringFormat();
    outCharacterFormat.Alignment = StringAlignment.Center;
    outCharacterFormat.LineAlignment = StringAlignment.Center;

    // painting the character
    e.Appearance.FontSizeDelta = -2;
    e.Appearance.FontStyleDelta = FontStyle.Bold;
    e.Cache.DrawString(displayCharacter, e.Appearance.Font, 
        e.Cache.GetSolidBrush(Color.White), rect, outCharacterFormat);

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

Private Sub TreeList1_CustomDrawNodeButton(ByVal sender As Object, _
ByVal e As CustomDrawNodeButtonEventArgs) Handles TreeList1.CustomDrawNodeButton
    Dim rect As Rectangle = Rectangle.Inflate(e.Bounds, 0, -2)
    ' painting background
    Dim BackBrush = e.Cache.GetGradientBrush(rect, Color.Blue, Color.LightSkyBlue,
                                             LinearGradientMode.ForwardDiagonal)
    e.Cache.FillRectangle(BackBrush, rect)
    ' painting borders
    e.Cache.DrawRectangle(e.Cache.GetPen(Color.LightGray), rect)

    ' determining the character to display
    Dim DisplayCharacter As String = "+"
    If e.Expanded Then DisplayCharacter = "-"
    ' formatting the output character
    Dim OutCharacterFormat = e.Appearance.GetStringFormat()
    OutCharacterFormat.Alignment = StringAlignment.Center
    OutCharacterFormat.LineAlignment = StringAlignment.Center

    ' painting the character
    e.Appearance.FontSizeDelta = -2
    e.Appearance.FontStyleDelta = FontStyle.Bold
    e.Cache.DrawString(DisplayCharacter, e.Appearance.Font,
                          e.Cache.GetSolidBrush(Color.White), rect, OutCharacterFormat)

    ' prohibiting default painting
    e.Handled = True
End Sub

See Also

CustomDrawNodeIndicator

CustomDrawNodeCell

CustomDrawNodeImages

TreeList Class

TreeList Members

DevExpress.XtraTreeList Namespace