这个示例展示了如何重置控件的外观属性值 (例如 XRControl.BackColorXRControl.ForeColorXRControl.BordersXRControl.Font 等等)。 当这些属性值被在报表控件中设置、但后来应从控件的父对象获取它们的取值时,就需要重置。 要学习更多关于重写外观属性值的内容,请参阅 外观属性 文档。

CodeCentralShow Me

在 DevExpress Code Central 数据库中可以找到完整的示例项目,网址是 http://www.devexpress.com/example=E933。 取决于目标平台类型 (ASP.NET、WinForms 等),可以在线运行本示例,或者下载自动可执行的示例。

C#CopyCode image复制代码
using System.Drawing;
using System.Drawing.Printing;
using DevExpress.XtraReports.UI;
// ...

private void xrLabel2_BeforePrint(object sender, PrintEventArgs e) {
    SetCustomStyle((XRLabel)sender);
}
 
private void xrLabel3_BeforePrint(object sender, PrintEventArgs e) {
    SetCustomStyle((XRLabel)sender);
    ResetStyle((XRLabel)sender);
}
 
private void SetCustomStyle(XRLabel label) {
    label.BackColor = Color.Red;
    label.BorderColor = Color.Blue;
    label.Borders = DevExpress.XtraPrinting.BorderSide.All;
    label.BorderWidth = 5;
    label.Font = new Font(label.Font, FontStyle.Bold);
    label.ForeColor = Color.White;
    label.Padding = 10;
    label.TextAlignment = DevExpress.XtraPrinting.TextAlignment.MiddleCenter;
}
 
private void ResetStyle(XRLabel label) {
    label.ResetBackColor();
    label.ResetBorderColor();
    label.ResetBorders();
    label.ResetBorderWidth();
    label.ResetFont();
    label.ResetForeColor();
    label.ResetPadding();
    label.ResetTextAlignment();
}
Visual BasicCopyCode image复制代码
Imports System.Drawing
Imports System.Drawing.Printing
Imports DevExpress.XtraReports.UI
' ...

Private Sub xrLabel2_BeforePrint(ByVal sender As Object, ByVal e As PrintEventArgs) _
Handles xrLabel2.BeforePrint
    SetCustomStyle(CType(sender, XRLabel))
End Sub
 
Private Sub xrLabel3_BeforePrint(ByVal sender As Object, ByVal e As PrintEventArgs) _
Handles xrLabel3.BeforePrint
    SetCustomStyle(CType(sender, XRLabel))
    ResetStyle(CType(sender, XRLabel))
End Sub
 
Private Sub SetCustomStyle(ByVal label As XRLabel)
    label.BackColor = Color.Red
    label.BorderColor = Color.Blue
    label.Borders = DevExpress.XtraPrinting.BorderSide.All
    label.BorderWidth = 5
    abel.Font = New Font(label.Font, FontStyle.Bold)
    label.ForeColor = Color.White
    label.Padding = 10
    label.TextAlignment = DevExpress.XtraPrinting.TextAlignment.MiddleCenter
End Sub
 
Private Sub ResetStyle(ByVal label As XRLabel)
    label.ResetBackColor()
    label.ResetBorderColor()
    label.ResetBorders()
    label.ResetBorderWidth()
    label.ResetFont()
    label.ResetForeColor()
    label.ResetPadding()
    label.ResetTextAlignment()
End Sub

Expand image参阅