下列代码展示了如何根据特定的条件,在布局视图中动态定制卡片字段取值的外观。

在本示例中接管了 LayoutView.CustomFieldValueStyle 事件来定制 Description 和 CategoryName 字段取值的外观。 只在 CategoryName 字段被设置为“Beverages”的卡片中,才以红色绘制这些字段的取值。

结果显示如下:

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

private void layoutView1_CustomFieldValueStyle(object sender, 
LayoutViewFieldValueStyleEventArgs e) {
    if (e.Column.FieldName != "Description" && e.Column.FieldName != "CategoryName") return;
    LayoutView view = sender as LayoutView;
    bool isBeverage = view.GetRowCellValue(e.RowHandle, "CategoryName").ToString() == 
        "Beverages";
    if (isBeverage)
        e.Appearance.ForeColor = Color.Red;
}
Visual BasicCopyCode image复制代码
Imports DevExpress.XtraGrid.Views.Layout.Events
Imports DevExpress.XtraGrid.Views.Layout

Private Sub LayoutView1_CustomFieldValueStyle(ByVal sender As System.Object, _
ByVal e As LayoutViewFieldValueStyleEventArgs) Handles LayoutView1.CustomFieldValueStyle
   If e.Column.FieldName <> "Description" AndAlso e.Column.FieldName <> "CategoryName" Then Return
   Dim view As LayoutView = TryCast(sender, LayoutView)
   Dim isBeverage As Boolean = view.GetRowCellValue(e.RowHandle, "CategoryName").ToString() = 
      "Beverages"
   If isBeverage Then
       e.Appearance.ForeColor = Color.Red
   End If
End Sub