Back to Devexpress

TreeList.CustomDrawNodeCell Event

windowsforms-devexpress-dot-xtratreelist-dot-treelist-15b46e5e.md

latest9.4 KB
Original Source

TreeList.CustomDrawNodeCell Event

Provides the ability to perform custom painting of node cells.

Namespace : DevExpress.XtraTreeList

Assembly : DevExpress.XtraTreeList.v25.2.dll

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

Declaration

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

Event Data

The CustomDrawNodeCell event's data class is CustomDrawNodeCellEventArgs. 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.
CellTextGets the painted cell’s display text.
CellValueGets the painted cell’s value.
ColumnGets the painted cell’s column.
EditPainterGets the editor’s painter object used to paint a cell.
EditViewInfoGets the editor’s view information.
FocusedGets a value indicating whether the painted cell has focus.
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

The event parameter provides all the information necessary to paint a node cell. The e.Node and e.Column parameter properties allow you to identify the node and column to which a cell belongs.

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

Note

Custom drawing of any kind is ignored in the following instances:

  • The user activates the cell’s editor.
  • When printing/exporting the XtraTreeList. If it’s necessary to dynamically change cell styles in the print/export output of the Tree List control, handle the TreeList.NodeCellStyle event.

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.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

See Also

CustomDrawFooter

CustomDrawFooterCell

CustomDrawRowFooter

CustomDrawRowFooterCell

CustomDrawNodeIndicator

NodeCellStyle

TreeList Class

TreeList Members

DevExpress.XtraTreeList Namespace