Accessing the Report Preview Model in LightSwitch
In this blog post I will explain another small feature of XtraReports for LightSwitch: the ability to access the ReportPreviewModel from code. For example, I will show how you can provide custom editors for report parameters in a LightSwitch application.
Starting with our next minor release, in addition to the capability of passing parameters from LightSwitch queries to reports, …
… we have also added a new CustomizeReportPreviewModel method, which can be specifically used for accessing the ReportPreviewModel.
The above code is generated automatically in a ReportPreviewScreen's code behind. You should not rename this method or modify its type and visibility scope (public void).
Now, let’s use a ComboBoxEdit (which is part of our DevExpress Editors for Silverlight) as a custom parameter editor.
using System.Collections.Generic; using DevExpress.Xpf.Editors; using DevExpress.Xpf.Printing; // ... namespace LightSwitchApplication { public partial class ReportPreviewScreen { public void CustomizeReportPreviewModel(ReportPreviewModel model) { model.CustomizeParameterEditors += model_CustomizeParameterEditors; } List<object> categories; void model_CustomizeParameterEditors(object sender, CustomizeParameterEditorsEventArgs e) { if (e.Parameter.Name == "CategoryName") { var editor = new ComboBoxEdit(); editor.ItemsSource = categories; editor.IsTextEditable = false; e.Editor = editor; e.BoundDataMember = "EditValue"; } } partial void ReportPreviewScreen_Activated() { this.ReportTypeName = "XtraReport1"; categories = new List<object>(); foreach (Category category in new DataWorkspace().NorthWindData.Categories) { categories.Add(category.CategoryName); } } } }
Here is the result!
Please feel free to leave your comments below.