How to: Use a Custom Editor for an XtraReport Parameter 如何:对 XtraReport 参数使用自定义编辑器
This example demonstrates how to specify a custom control used to edit a report parameter value when a report is being previewed.
此示例演示如何指定用于在预览报表时编辑报表参数值的自定义控件。
Note 注意
Mobile applications do not support the report preview mechanism, so the approach described in this topic cannot be implemented in the Mobile platform.
移动应用程序不支持报表预览机制,因此本主题中描述的方法无法在移动平台中实现。
In this topic, it is assumed that you have an XAF application that uses the Reports V2 Module, and you have created one or more reports (see Reports V2 Module Overview).
在本主题中,假定您有一个使用报表 V2 模块的 XAF 应用程序,并且您创建了一个或多个报表(请参阅报表 V2 模块概述)。
Register the Parameter Type
注册参数类型
For instance, assume you have a report with the parameterTitle parameter of the TitleOfCourtesy type. Open the code of your report and specify the Parameter.Type value in the report's constructor.
例如,假设您有一个具有 TitleThe 类型参数的报表。打开报表的代码,并在报表的构造函数中指定参数.Type 值。
public partial class XtraReport1 : DevExpress.XtraReports.UI.XtraReport { public XtraReport1() { InitializeComponent(); this.parameterTitle.Type = typeof(TitleOfCourtesy); } // ... }
If the registered type is not a standard .NET type (string, int, etc.) and is not used for business object properties, it is also necessary to register this type by using the approach shown in the How to: Register an Additional Type of XtraReport Parameter topic.
如果注册类型不是标准 .NET 类型(字符串、int 等),并且不用于业务对象属性,则还需要使用"如何:注册其他类型的 XtraReport 参数"主题中显示的方法注册此类型。
Specify a Custom Editor for Parameters of a Standard .NET Type in a WinForms Report Viewer
在 WinForms 报表查看器中为标准 .NET 类型的参数指定自定义编辑器
To provide a custom editor for parameters of a standard type in a Windows Forms application, handle the XtraReport.ParametersRequestBeforeShow event, and assign the custom editor to the ParameterInfo.Editor property of the ParameterInfo object stored in the ParametersRequestEventArgs.ParametersInformation collection. To get an XtraReport instance, handle the ReportDataSourceHelper.BeforeShowPreview event.
要为 Windows 窗体应用程序中的标准类型的参数提供自定义编辑器,请处理 XtraReport.参数请求之前显示事件,并将自定义编辑器分配给存储在 中的参数信息的参数信息编辑器属性。参数请求事件Args.参数信息集合。要获取 XtraReport 实例,需要处理报表数据源帮助程序。
using DevExpress.ExpressApp; using DevExpress.ExpressApp.ReportsV2; using DevExpress.XtraEditors; using DevExpress.XtraReports.Parameters; // ... public class WinModule : ModuleBase { public override void Setup(ApplicationModulesManager moduleManager) { base.Setup(moduleManager); ReportsModuleV2 module = ReportsModuleV2.FindReportsModule(moduleManager.Modules); if(module != null) { module.ReportsDataSourceHelper.BeforeShowPreview += ReportsDataSourceHelper_BeforeShowPreview; } } private void ReportsDataSourceHelper_BeforeShowPreview(object sender, BeforeShowPreviewEventArgs e) { e.Report.ParametersRequestBeforeShow += (s, arg) => { foreach(ParameterInfo info in arg.ParametersInformation) { if(info.Parameter.Name == "parameter1") { LookUpEdit lookUpEdit = new LookUpEdit(); lookUpEdit.Properties.DataSource = new List<string>(new string[] { "One", "Two"}); info.Editor = lookUpEdit; } } }; } }
Specify a Custom Editor for Parameters of a Custom Type in a WinForms Report Viewer
在 WinForms 报表查看器中为自定义类型的参数指定自定义编辑器
Handle the static ReportsWindowsFormsModuleV2.CreateCustomReportDesignRepositoryItem event to specify a custom RepositoryItem used to edit a parameter value when a report is being previewed. Pass your repository item using the handler's CreateCustomReportDesignRepositoryItemEventArgs.RepositoryItem parameter and set the Handled parameter to true. The specified control will be used for any report in the application.
处理静态报表WindowsFormsModuleV2.创建自定义报表设计存储库项目事件,以指定用于在预览报表时编辑参数值的自定义存储库项目。使用处理程序的"创建自定义报表设计"项目项目项目.存储库项目参数传递存储库项,并将"已处理"参数设置为 true。指定的控件将用于应用程序中的任何报表。
As the CreateCustomReportDesignRepositoryItem is static, which means that you can access it anywhere in your WinForms project. For instance, you can subscribe to the CreateCustomReportDesignRepositoryItem in the overridden ModuleBase.Setup method of a WinForms module project (in the WinModule.cs (WinModule.vb) file).
由于创建自定义报表设计存储库项目是静态的,这意味着您可以在 WinForms 项目中的任意位置访问它。例如,您可以在重写的 ModuleBase.WinForms 模块项目的设置方法(在WinModule.cs (WinModule.vb) 文件中)订阅创建自定义报表设计存储库项目。
using DevExpress.XtraEditors.Repository; using DevExpress.ExpressApp.ReportsV2.Win; // ... public override void Setup(XafApplication application) { base.Setup(application); ReportsWindowsFormsModuleV2.CreateCustomReportDesignRepositoryItem += delegate(object sender, CreateCustomReportDesignRepositoryItemEventArgs e) { if(e.Parameter.Name.Equals("parameterTitle")) { RepositoryItemLookUpEdit item = new RepositoryItemLookUpEdit(); item.NullText = "[Select Title Of Courtesy]"; List<TitleOfCourtesy> st = new List<TitleOfCourtesy>(); st.Add(TitleOfCourtesy.Dr); st.Add(TitleOfCourtesy.Mrs); item.DataSource = st; e.RepositoryItem = item; e.Handled = true; } }; }
Specify a Custom Editor for the HTML5 Report Viewer
为 HTML5 报表查看器指定自定义编辑器
This approach is applicable when the HTML5 Document Viewer is used to preview reports (the ReportsAspNetModuleV2.ReportViewerType property is set to HTML5).
当 HTML5 文档查看器用于预览报表时,此方法适用(报表AspNetModuleV2.报表查看器类型属性设置为 HTML5)。
Define a custom template for a parameter editor in the Default.aspx file of an ASP.NET application project. (See Providing Custom Editors for Report Parameters)
在ASP.NET应用程序的 Default.aspx 文件中为参数编辑器定义自定义模板。(请参阅为报表参数提供自定义编辑器)
- ASPX
<script type ="text/html" id="catId-custom-editor"> <div data-bind="dxNumberBox: { value: value, showSpinButtons: true, min: 1, max: 8 }"> </div> </script>
Handle the ASPxClientWebDocumentViewer.CustomizeParameterEditors event of the Document Viewer's client-side instance.
处理 ASPxClientWeb 文档查看器.自定义文档查看器的客户端实例的参数编辑器事件。
using DevExpress.ExpressApp; using DevExpress.ExpressApp.ReportsV2.Web; // ... public class CustomizeReportViewerController : ViewController<DetailView> { public CustomizeReportViewerController() { TargetViewId = ReportsAspNetModuleV2.ReportViewDetailViewWebName; } protected override void OnActivated() { base.OnActivated(); ReportWebViewerDetailItem reportViewItem = View.GetItems<ReportWebViewerDetailItem>()[0] as ReportWebViewerDetailItem; reportViewItem.ControlCreated += delegate (object sender, EventArgs e) { reportViewItem.ReportViewer.ClientSideEvents.CustomizeParameterEditors = @"function(s, e) { if (e.parameter.name == 'parameter1') { e.info.editor = { header: 'catId-custom-editor' }; } }"; }; } }
Specify a Custom Editor for the ASP.NET Report Viewer
为ASP.NET报表查看器指定自定义编辑器
This approach is applicable when the ASP.NET Document Viewer is used to preview reports (the ReportsAspNetModuleV2.ReportViewerType property is set to ASP).
当使用ASP.NET文档查看器预览报表(报表AspNetModuleV2.报表查看器类型属性设置为 ASP)时,此方法适用。
Important 重要
In v19.1, the ASP.NET Document Viewer has become deprecated. We recommend that you use the HTML5 Document Viewer in your applications. For this, set the ReportViewerType property to HTML5.
在 v19.1 中,ASP.NET 文档查看器已弃用。我们建议您在应用程序中使用 HTML5 文档查看器。为此,将报表查看器类型属性设置为 HTML5。
Create a View Controller in the ASP.NET module project. Override the Controller's OnActivated method, access the WebReportServiceController using the Frame.GetController<ControllerType> method and subscribe to the WebReportServiceController.CustomizeParameterEditors event. Pass your custom control to the handler's CustomizeParameterEditorsEventArgs.Editor parameter. The specified control will be used for any report in the application.
在ASP.NET模块项目中创建视图控制器。覆盖控制器的 On 已激活方法,使用 Frame.GetController<ControllerType> 方法访问 Web 报表服务控制器,并订阅 Web 报表服务控制器。自定义参数编辑器事件。将自定义控件传递给处理程序的自定义参数编辑器EventArgs.编辑器参数。指定的控件将用于应用程序中的任何报表。
using DevExpress.Web.ASPxEditors; using DevExpress.XtraReports.Web; using DevExpress.ExpressApp; using DevExpress.ExpressApp.ReportsV2.Web; // ... public class CustomReportParametersEditorController : ViewController{ private WebReportServiceController controller; protected override void OnActivated() { base.OnActivated(); controller = Frame.GetController<WebReportServiceController>(); if (controller != null) { controller.CustomizeParameterEditors += delegate(object sender, CustomizeParameterEditorsEventArgs e) { if(e.Parameter.Name.Equals("parameterTitle")) { ASPxComboBox comboBox = new ASPxComboBox(); comboBox.Items.Add("Dr",TitleOfCourtesy.Dr); comboBox.Items.Add("Mr",TitleOfCourtesy.Mr); e.Editor = comboBox; } }; } }
XAF开发成品案例参考
如果您觉得阅读本文对您有帮助,请点一下“推荐”按钮,您的“推荐”将是我最大的写作动力!欢迎各位转载,但是未经作者本人同意,转载文章之后必须在文章页面明显位置给出作者和原文连接,否则保留追究法律责任的权利。
作者博客: http://www.cnblogs.com/foreachlife
欢迎加入CIIP框架\XAF技术应用交流群: 336090194 群文件中有更多相关工具及文档资料
转载请注明出处。多谢!
欢迎加我微信: admiralcn 或扫码: