本示例展示了如何使用 CardView.CustomDrawCardFieldCaption 事件来定制 卡片字段标题。 对获得焦点的和没有获得焦点的字段绘制不同的字段标题。

C#CopyCode image复制代码
using DevExpress.XtraGrid.Views.Base;

private void cardView1_CustomDrawCardFieldCaption(object sender, RowCellCustomDrawEventArgs e) {
   CardView cv = sender as CardView;
   bool isFocused = false;
   if(cv.FocusedColumn != null)
      isFocused = e.Column.AbsoluteIndex == cv.FocusedColumn.AbsoluteIndex && 
        e.RowHandle == cv.FocusedRowHandle;
   //A brush to draw the background of the card field caption
   Brush backBrush;
   if (isFocused)
      backBrush = e.Cache.GetGradientBrush(e.Bounds, Color.LavenderBlush, Color.Navy, 
        LinearGradientMode.Vertical);
   else
      backBrush = e.Cache.GetGradientBrush(e.Bounds, Color.Aquamarine, Color.DarkSeaGreen, 
        LinearGradientMode.Vertical);
   Rectangle r = e.Bounds;
   //Draw a 3D border
   ControlPaint.DrawBorder3D(e.Graphics, r, Border3DStyle.RaisedInner);
   r.Inflate(-1, -1);
   //Fill the background
   e.Graphics.FillRectangle(backBrush, r);
   r.Y--;
   //Change the appearance for drawing the focused field's caption
   if(isFocused) {            
      e.Appearance.ForeColor = Color.White;
      e.Appearance.TextOptions.HAlignment = HorzAlignment.Center;
   }
   e.Appearance.DrawString(e.Cache, e.DisplayText, r);
   //Prevent default painting
   e.Handled = true;
}

Visual BasicCopyCode image复制代码
Imports DevExpress.XtraGrid.Views.Base

Private Sub CardView1_CustomDrawCardFieldCaption(ByVal sender As Object, _
ByVal e As .RowCellCustomDrawEventArgs) Handles CardView1.CustomDrawCardFieldCaption
   Dim cv As CardView = CType(sender, CardView)
   Dim isFocused As Boolean = False
   If Not cv.FocusedColumn Is Nothing Then isFocused = e.Column.AbsoluteIndex = _
cv.FocusedColumn.AbsoluteIndex And e.RowHandle = cv.FocusedRowHandle
   'A brush to draw the background of the card field caption
   Dim backBrush As Brush
   If isFocused Then
      backBrush = e.Cache.GetGradientBrush(e.Bounds, Color.LavenderBlush, Color.Navy, _
        LinearGradientMode.Vertical)
   Else
      backBrush = e.Cache.GetGradientBrush(e.Bounds, Color.Aquamarine, Color.DarkSeaGreen, _
        LinearGradientMode.Vertical)
   End If

   Dim r As Rectangle = e.Bounds
   'Draw a 3D border
   ControlPaint.DrawBorder3D(e.Graphics, r, Border3DStyle.RaisedInner)
   r.Inflate(-1, -1)
   'Fill the background
   e.Graphics.FillRectangle(backBrush, r)
   r.Y -= 1
   'Change the appearance for drawing the focused field's caption
   If isFocused Then
      e.Appearance.ForeColor = Color.White
      e.Appearance.TextOptions.HAlignment = HorzAlignment.Center
   End If
   e.Appearance.DrawString(e.Cache, e.DisplayText, r)
   'Prevent default painting
   e.Handled = True
End Sub