定制个别单元格的外观
可以通过接管 TreeList.NodeCellStyle 事件来定制个别单元格的外观。 在单元格被绘制之前,此事件为每个单元格发生。
改变单元格外观的其他方式是接管 TreeList.CustomDrawNodeCell 事件。 注意,通过自定义绘制事件改变的元素外观在打印和导出控件时不起作用。 要获得更多关于实现自定义绘制的信息,请参阅 自定义绘制
示例
下面的示例代码接管了 TreeList.NodeCellStyle 事件来修改“Budget”列中取值大于 500,000 的单元格的外观。
下面的插图展示了运行结果。
C# | 复制代码 |
---|---|
using DevExpress.XtraTreeList; private void treeList1_NodeCellStyle(object sender, GetCustomNodeCellStyleEventArgs e) { // Modifying the appearance settings used to paint the "Budget" column's cells // whose values are greater than 500,000 . if (e.Column.FieldName != "Budget") return; if (Convert.ToInt32(e.Node.GetValue(e.Column.AbsoluteIndex)) > 500000) { e.Appearance.BackColor = Color.FromArgb(80, 255, 0, 255); e.Appearance.ForeColor = Color.White; e.Appearance.Font = new Font(e.Appearance.Font, FontStyle.Bold); } } |
Visual Basic | 复制代码 |
---|---|
Imports DevExpress.XtraTreeList Private Sub TreeList1_NodeCellStyle(ByVal sender As Object, _ ByVal e As GetCustomNodeCellStyleEventArgs) Handles TreeList1.CustomNodeCellStyle ' Modifying the appearance settings used to paint the "Budget" column's cells ' whose values are greater than 500,000 . If e.Column.FieldValue <> "Budget" Exit Sub If Convert.ToInt32(e.Node.GetValue(BudgetColumnID)) > 500000 Then e.Appearance.BackColor = Color.FromArgb(80, 255, 0, 255) e.Appearance.ForeColor = Color.White e.Appearance.Font = New Font(e.Appearance.Font, FontStyle.Bold) End If End Sub |
示例
下面的示例代码接管了 TreeList.CustomDrawNodeCell 事件。 此事件用于执行节点单元格的自定义绘制。 以相同的方式绘制所有节点单元格。 但是,可以根据节点设置执行不同的绘制。 请参阅 CustomDraw 演示获得更复杂的节点绘制示例。
下面的插图显示了示例代码的运行结果。
C# | 复制代码 |
---|---|
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 Basic | 复制代码 |
---|---|
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 |