下面的示例代码接管了 TreeList.CustomDrawNodeCell 事件。 此事件用于执行节点单元格的自定义绘制。 以相同的方式绘制所有节点单元格。 但是,可以根据节点设置执行不同的绘制。 请参阅 CustomDraw 演示获得更复杂的节点绘制示例。

下面的插图显示了示例代码的运行结果。

C#CopyCode image复制代码
using System.Drawing;
using System.Drawing.Drawing2D;
using DevExpress.XtraTreeList;

private void treeList1_CustomDrawNodeCell(object sender, CustomDrawNodeCellEventArgs e) {
   // obtaining brushes for cells of focused and unfocused nodes
   Brush backBrush, foreBrush;
   if (e.Node != (sender as TreeList).FocusedNode) {
      backBrush = new LinearGradientBrush(e.Bounds, Color.Orange, Color.PeachPuff, 
        LinearGradientMode.Horizontal);
      foreBrush = new SolidBrush(Color.Black);
   }
   else {
      backBrush = new SolidBrush(Color.DarkBlue);
      foreBrush = new SolidBrush(Color.PeachPuff);
   }
   // filling the background
   e.Graphics.FillRectangle(backBrush, e.Bounds);
   // painting node value
   e.Graphics.DrawString(e.CellText, e.Appearance.Font, foreBrush, e.Bounds, 
     e.Appearance.GetStringFormat());

   // prohibiting default painting
   e.Handled = true;
}
Visual BasicCopyCode image复制代码
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports DevExpress.XtraTreeList

Private Sub TreeList1_CustomDrawNodeCell(ByVal sender As Object, _
ByVal e As CustomDrawNodeCellEventArgs) Handles TreeList1.CustomDrawNodeCell
   ' obtaining brushes for cells of focused and unfocused nodes
   Dim BackBrush, ForeBrush As Brush
   If e.Node.Id <> sender.FocusedNode.Id Then
      BackBrush = New LinearGradientBrush(e.Bounds, Color.Orange, Color.PeachPuff, _
        LinearGradientMode.Horizontal)
      ForeBrush = New SolidBrush(Color.Black)
   Else
      BackBrush = New SolidBrush(Color.DarkBlue)
      ForeBrush = New SolidBrush(Color.PeachPuff)
   End If
   ' filling the background
   e.Graphics.FillRectangle(BackBrush, e.Bounds)
   ' painting node value
   Dim ValueRect As New RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, _
     e.Bounds.Height)
   e.Graphics.DrawString(e.CellText, e.Appearance.Font, ForeBrush, ValueRect, _
     e.Appearance.GetStringFormat())

   ' prohibiting default painting
   e.Handled = True
End Sub