注意 |
---|
重要说明: .NET Client Profile Framework 不支持此功能。 要在最终用户的机器上使用此功能,则必须安装完整的 .NET Framework。 更多信息,请参阅 Windows 窗体部署 文档中的 关于 .NET Framework 4.0 Client Profile 的重要说明 小节。 |
本文档提供了对 标准 MDI 最终用户报表设计器窗体进行定制的示例。
时常需要定制默认的最终用户设计器的界面。 使用 XtraReports,可以轻松解决许多这样的任务,而不需要从零开始 组装自定义报表设计器。 在下面提供了一些进行定制的典型示例。
定制设计器的布局
标准的最终用户设计器包含了多种停靠面板 (在 DesignDockPanelType 枚举中列出),可以通过 XRDesignForm.SetWindowVisibility 方法来隐藏。
这个示例展示了如何使用 XRDesignForm 类,来把报表加载到 最终用户报表设计器 窗体中。
为了把报表指派到设计器窗体,使用了 XRDesignForm.OpenReport 方法。 同时,XRDesignForm.SetWindowVisibility 方法隐藏了所创建设计器窗体中的 Field List(字段列表) 和 属性网格 设计停靠面板。
C# | 复制代码 |
---|---|
using System; using System.Windows.Forms; using DevExpress.XtraReports.UI; using DevExpress.XtraReports.UserDesigner; // ... private void button1_Click(object sender, EventArgs e) { // Create a design form. XRDesignForm designForm = new XRDesignForm(); // Open the report to edit. designForm.OpenReport(new XtraReport1()); // Hide the Field List and Property Grid dock panels. designForm.SetWindowVisibility(DesignDockPanelType.FieldList | DesignDockPanelType.PropertyGrid, false); // Invoke the design form. designForm.Show(); } |
Visual Basic | 复制代码 |
---|---|
Imports System Imports System.Windows.Forms Imports DevExpress.XtraReports.UI Imports DevExpress.XtraReports.UserDesigner ' ... Private Sub OnButtonClick(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click ' Create a design form. Dim DesignForm As New XRDesignForm() ' Open the report to edit. DesignForm.OpenReport(New XtraReport1()) ' Hide the Field List and Property Grid dock panels. DesignForm.SetWindowVisibility(DesignDockPanelType.FieldList Or _ DesignDockPanelType.PropertyGrid, False) ' Invoke the design form. DesignForm.Show() End Sub |
或者,也可以选择使用 XtraReport.ShowDesigner 方法、并把 DesignDockPanelType.FieldList 和 DesignDockPanelType.PropertyGrid 字段传递为参数,从而达到相同的效果。
C# | 复制代码 |
---|---|
XtraReport1 report = new XtraReport1(); // Invoke the End-User Designer without PropertyGrid and ReportExplorer panels. report.ShowDesigner(UserLookAndFeel.Default, DesignDockPanelType.FieldList | DesignDockPanelType.PropertyGrid); |
Visual Basic | 复制代码 |
---|---|
Dim report As New XtraReport1() ' Invoke the End-User Designer without PropertyGrid and ReportExplorer panels. report.ShowDesigner(UserLookAndFeel.Default, _ DesignDockPanelType.FieldList Or DesignDockPanelType.PropertyGrid) |
定制工具箱
这个示例展示了如何把自定义控件添加到最终用户设计器的 工具箱 中。
要这样做,则接管 XRDesignMdiController.DesignPanelLoaded 事件处理程序,然后把所需的项添加到工具箱中。
要使本示例能正确工作,则 XtraReport1 子类(派生于 XtraReport 类) 应该存在于示例应用程序中。
C# | 复制代码 |
---|---|
using System; using System.Windows.Forms; using System.Drawing.Design; using DevExpress.XtraReports.UI; using DevExpress.XtraReports.UserDesigner; // ... private void button1_Click(object sender, EventArgs e) { // Create an End-User Designer form. XRDesignForm designForm = new XRDesignForm(); // Handle the DesignPanelLoaded event. designForm.DesignMdiController.DesignPanelLoaded += new DesignerLoadedEventHandler(DesignMdiController_DesignPanelLoaded); // Load a report into the Designer. designForm.OpenReport(new XtraReport1()); // Show the End-User Designer form, modally. designForm.ShowDialog(); } void DesignMdiController_DesignPanelLoaded(object sender, DesignerLoadedEventArgs e) { // Get the Toolbox service from the Designer host. IToolboxService ts = (IToolboxService)e.DesignerHost.GetService(typeof(IToolboxService)); // Add a new category to the Toolbox, // and place a custom control into it. ts.AddToolboxItem(new ToolboxItem(typeof(MyControl)), "New Category"); } // A custom control. public class MyControl : XRControl { // ... } |
Visual Basic | 复制代码 |
---|---|
Imports System Imports System.Windows.Forms Imports System.Drawing.Design Imports DevExpress.XtraReports.UI Imports DevExpress.XtraReports.UserDesigner ' ... Private Sub button1_Click(ByVal sender As Object, _ ByVal e As EventArgs) Handles button1.Click ' Create an End-User Designer form. Dim designForm As New XRDesignForm() ' Handle the DesignPanelLoaded event. AddHandler designForm.DesignMdiController.DesignPanelLoaded, _ AddressOf DesignMdiController_DesignPanelLoaded ' Load a report into the Designer. designForm.OpenReport(New XtraReport1()) ' Show the End-User Designer form, modally. designForm.ShowDialog() End Sub Private Sub DesignMdiController_DesignPanelLoaded(ByVal sender As Object, _ ByVal e As DesignerLoadedEventArgs) ' Get the Toolbox service from the Designer host. Dim ts As IToolboxService = _ CType(e.DesignerHost.GetService(GetType(IToolboxService)), IToolboxService) ' Add a new category to the Toolbox, ' and place a custom control into it. ts.AddToolboxItem(New ToolboxItem(GetType(MyControl)), "New Category") End Sub ' A custom control. Public Class MyControl Inherits XRControl ' ... End Class |
定制属性网格
这个示例展示了如何隐藏 最终用户设计器 的属性网格中的某些属性。 要这样做,则接管 XtraReport.FilterControlProperties 静态事件,并为属性网格定义筛选规则。
例如,下列代码演示了如何隐藏 XRControl.Scripts 属性 (对所有报表元素隐藏)、SubreportBase.ReportSource 属性 (对所有子报表隐藏)、 XRControl.Name 和 XRControl.DataBindings 属性 (对特定标签隐藏)。
C# | 复制代码 |
---|---|
using DevExpress.XtraReports.UI; using DevExpress.XtraReports.UserDesigner; // ... static void Main() { // Handle the static FilterControlProperties event // to define filtering rules for the Property Grid. XtraReport.FilterControlProperties += new FilterControlPropertiesEventHandler(OnFilterControlProperties); Application.Run(new Form1()); } private void button1_Click(object sender, System.EventArgs e) { // Create a report of the XtraReport1 type and invoke its End-User Designer. XtraReport1 report = new XtraReport1(); report.ShowDesigner(); } static void OnFilterControlProperties(object sender, FilterControlPropertiesEventArgs e) { // If you wish to hide the scripting ability in the End-User Designer, // just remove the Scripts property for all report elements. e.Properties.Remove("Scripts"); // If you wish subreports to always show the predefined reports, // you may hide the ReportSource property. if (e.Control is XRSubreport) { e.Properties.Remove("ReportSource"); } // The following code hides the Name and DataBindings properties // for the xrLabel1 in XtraReport1. if (sender is XtraReport1 && e.Control.Name == "xrLabel1") { e.Properties.Remove("Name"); e.Properties.Remove("DataBindings"); } } |
Visual Basic | 复制代码 |
---|---|
Imports DevExpress.XtraReports.UI Imports DevExpress.XtraReports.UserDesigner ' ... ' NOTE: To make this code work, set the "Startup object" option ' to "Sub Main" in the General properties of your project. <STAThread()> Shared Sub Main() ' Handle the static FilterControlProperties event ' to define filtering rules for the Property Grid. AddHandler XtraReport.FilterControlProperties, AddressOf OnFilterControlProperties Application.Run(New Form1()) End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles Button1.Click ' Create a report of the XtraReport1 type and invoke its End-User Designer. Dim report As New XtraReport1() report.ShowDesigner() End Sub Shared Sub OnFilterControlProperties(ByVal sender As System.Object, ByVal e As _ FilterControlPropertiesEventArgs) ' If you wish to hide the scripting ability in the End-User Designer, ' just remove the Scripts property for all report elements. e.Properties.Remove("Scripts") ' If you wish subreports to always show the predefined reports, ' you may hide the ReportSource property. If TypeOf e.Control Is XRSubreport Then e.Properties.Remove("ReportSource") End If ' The following code hides the Name and DataBindings properties ' for the xrLabel1 in XtraReport1. If TypeOf sender Is XtraReport1 And e.Control.Name = "XrLabel1" Then e.Properties.Remove("Name") e.Properties.Remove("DataBindings") End If End Sub |
定制字段列表
在报表设计器中,Field List(字段列表) 允许最终用户把数据绑定控件添加到报表中。 它显示了绑定数据源的数据结构。 有时,可能需要改变表或字段在“字段列表”中的显示名称。
这个示例展示了如何使用 IDataDictionary 接口,来改变显示在 IDE 报表设计器或 最终用户设计器 中的 Field List(字段列表) 项的名称。 展示了 DataSet 类的子类如何实现 IDataDictionary 接口。 那样,将把实现了 IDataDictionary 接口的数据源中的名称,自动取回到“字段列表”中。
注意 |
---|
如果 IDataDictionary.GetObjectDisplayName 方法返回 null (在 Visual Basic 中为 Nothing) 或者 String.Empty,那么这些数据项将被隐藏。 |
下面的插图举例说明了“字段列表”在实现 IDataDictionary 接口之前(左图) 和之后(右图) 的外观。
任何被用作报表数据源的对象都可以实现此接口。
C# | 复制代码 |
---|---|
using System; using DevExpress.Data; // ... public class DataSet1 : DataSet, IDataDictionary { //... // Implement the GetDataSourceDisplayName method. string IDataDictionary.GetDataSourceDisplayName() { return "Northwind Traders"; } // Implement the GetObjectDisplayName method. string IDataDictionary.GetObjectDisplayName(string dataMember) { // Hide the data member, which name ends with 'ID'. if(dataMember.EndsWith("ID")) { return null; } // Hide the 'Products' table, as its fields are accessible via // the 'CategoriesProducts' table only. if(dataMember.StartsWith("Products")) { return null; } // Find a dot in the data member's name. string[] names = dataMember.Split('.'); // Get a field name form the data member's name. string fieldName = names[names.Length - 1]; // Insert spaces between separate words of a field name. return ChangeNames(fieldName); } public string ChangeNames(string name) { string result = string.Empty; bool isPrevLow = false; foreach(char symb in name) { // Check if a character is of upper case. To avoid spaces inside abbreviations, // check if the previous character is of upper case, too. if(Char.IsUpper(symb) && isPrevLow) { result += " " + symb; } else { result += symb; } isPrevLow = Char.IsLower(symb); } return result; } // ... } |
Visual Basic | 复制代码 |
---|---|
Imports System Imports DevExpress.Data ' ... Public Class DataSet1 Inherits DataSet Implements IDataDictionary '... ' Implement the GetDataSourceDisplayName method. Function GetDataSourceDisplayName() As String _ Implements IDataDictionary.GetDataSourceDisplayName Return "Northwind Traders" End Function ' Implement the GetObjectDisplayName method. Function GetObjectDisplayName(ByVal dataMember As String) As String _ Implements IDataDictionary.GetObjectDisplayName ' Hide the data member, which name ends with 'ID'. If dataMember.EndsWith("ID") Then Return Nothing End If ' Hide the 'Products' table, as its fields are accessible via ' the 'CategoriesProducts' table only. If dataMember.StartsWith("Products") Then Return Nothing End If ' Find a dot in the data member's name. Dim names As String() = dataMember.Split("."c) ' Get a field name form the data member's name. Dim fieldName As String = names((names.Length - 1)) ' Insert spaces between separate words of a field name. Return ChangeNames(fieldName) End Function Public Function ChangeNames(ByVal name As String) As String Dim result As String = String.Empty Dim isPrevLow As Boolean = False Dim symb As Char For Each symb In name ' Check if a character is of upper case. To avoid spaces inside abbreviations, ' check if the previous character is of upper case, too. If [Char].IsUpper(symb) And isPrevLow Then result += " " + symb Else result += symb End If isPrevLow = [Char].IsLower(symb) Next symb Return result End Function ' ... End Class |