Back to Devexpress

Painters

windowsforms-5662-controls-and-libraries-tree-list-feature-center-appearances-and-look-and-feel-custom-drawing-painters.md

latest7.1 KB
Original Source

Painters

  • Jan 23, 2019
  • 3 minutes to read

The Tree List control’s elements can be custom painted. This can be done by using either the custom draw events designed for this purpose, or by providing your own Painter class for the control. This section contains information on creating a custom Painter class.

Tree List Painters

Tree List painters are objects implementing the ITreeListPaintHelper interface (the following section describes this interface). To create a custom painter, you need to create a class that implements the ITreeListPaintHelper interface. Alternatively, you can create a custom painter class, deriving it from the TreeList’s default painter - the DevExpress.XtraTreeList.Painter.TreeListPaintHelper class

To use a custom painter, assign an instance of this class to the DefaultPainter property of the TreeList.Painter object. This property refers to an instance of the painter class that is used to paint the TreeList’s elements. Initially, this property refers to a TreeListPaintHelper object.

ITreeListPaintHelper Interface

Tree List paint helpers must support the ITreeListPaintHelper interface. This interface declares methods that paint particular types of elements. The code below is the declaration of this interface.

public interface ITreeListPaintHelper { void DrawOpenCloseButton(CustomDrawNodeButtonEventArgs e); void DrawColumn(CustomDrawColumnHeaderEventArgs e); void DrawNodePreview(CustomDrawNodePreviewEventArgs e); void DrawNodeSelectImage(CustomDrawNodeImagesEventArgs e); void DrawNodeStateImage(CustomDrawNodeImagesEventArgs e); void DrawIndicator(CustomDrawNodeIndicatorEventArgs e); void DrawFooterBackGround(CustomDrawEventArgs e); void DrawRowFooterCell(CustomDrawRowFooterCellEventArgs e); void DrawFooterCell(CustomDrawFooterCellEventArgs e); void DrawNodeCell(CustomDrawNodeCellEventArgs e); }

The following table lists the methods of this interface, provides a brief description for each of them and lists the custom painting events that can be used instead.

MethodDescriptionCustomDraw Event
DrawOpenCloseButtonPaints group buttons. Gets a CustomDrawNodeButtonEventArgs object as the parameter.TreeList.CustomDrawNodeButton
DrawColumnPaints elements of the column header panel. Gets a CustomDrawColumnHeaderEventArgs object as the parameter.TreeList.CustomDrawColumnHeader
DrawNodePreviewPaints preview sections. Gets a CustomDrawNodePreviewEventArgs object as the parameter.TreeList.CustomDrawNodePreview
DrawNodeSelectImagePaints select images. Gets a CustomDrawNodeImagesEventArgs object as the parameter.TreeList.CustomDrawNodeImages
DrawNodeStateImagePaints state images. Gets a CustomDrawNodeImagesEventArgs object as the parameter.TreeList.CustomDrawNodeImages
DrawIndicatorPaints the indicator cells. Gets a CustomDrawNodeIndicatorEventArgs object as the parameter.TreeList.CustomDrawNodeIndicator
DrawFooterBackGroundPaints the summary footer’s background. Gets a CustomDrawEventArgs object as the parameter.TreeList.CustomDrawFooter
DrawRowFooterCellPaints row footer cells. Gets a CustomDrawRowFooterCellEventArgs object as the parameter.TreeList.CustomDrawRowFooterCell
DrawFooterCellPaints summary footer cells. Gets a CustomDrawFooterCellEventArgs object as the parameter.TreeList.CustomDrawFooterCell
DrawNodeCellPaints the node cells. Gets a CustomDrawNodeCellEventArgs object as the parameter.TreeList.CustomDrawNodeCell

Example

The code below creates a simple paint helper, deriving it from the standard TreeListPaintHelper class. The DrawFooterBackGround method is overridden to custom paint the summary footer.

The image below shows the Tree List control before and after the code has been executed.

csharp
using DevExpress.Utils;
using DevExpress.XtraTreeList;
using DevExpress.XtraTreeList.Painter;
using System.Drawing.Drawing2D;
// ...
public class SimplePaintHelper : TreeListPaintHelper {
    internal SimplePaintHelper(ImageList il_Indicator) : base() { }
    public override void DrawFooterBackGround(CustomDrawEventArgs e) {
        LinearGradientBrush brush = new LinearGradientBrush(e.Bounds, Color.FromArgb(244, 210, 227), Color.FromArgb(207, 196, 230), LinearGradientMode.Vertical);
        using (brush) {
            e.Cache.FillRectangle(brush, e.Bounds);
        }
    }
}

//...
// Use the painter:
treeList1.Painter.DefaultPaintHelper = new SimplePaintHelper(treeList1.Painter.IndicatorImages);
vb
Imports DevExpress.Utils
Imports DevExpress.XtraTreeList
Imports DevExpress.XtraTreeList.Painter
Imports System.Drawing.Drawing2D
' ...
Public Class SimplePaintHelper
   Inherits TreeListPaintHelper

   Friend Sub New(ByVal il_Indicator As ImageList)
      MyBase.New(il_Indicator)
   End Sub

   Public Overrides Sub DrawFooterBackGround(ByVal e As CustomDrawEventArgs)
      Dim Brush As New LinearGradientBrush(e.Bounds, Color.FromArgb(244, 210, 227), Color.FromArgb(207, 196, 230), LinearGradientMode.Vertical)
      Using Brush
          e.Cache.FillRectangle(Brush, e.Bounds)
      End Using
   End Sub
End Class

'...
' Use the painter:
TreeList1.Painter.DefaultPaintHelper = New SimplePaintHelper(TreeList1.Painter.IndicatorImages)