翻譯|使用教程|編輯:楊鵬連|2021-02-20 10:24:42.417|閱讀 328 次
概述:該ReportEditor控件可用于創建、打開、保存、預覽和打印MindFusion.Reporting報表。它可以集成在任何WinForms應用程序中,并為其提供編輯報表的能力。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
MindFusion.Reporting for WinForms是一個原生的Windows Forms編程組件,它為任何.NET應用程序提供專業的報表功能。該組件完全使用C#語言編寫,易于使用和集成。它提供您創建一個完美報表所需要的一切。MindFusion.Reporting for WinForms現已加入在線訂購,Standard Single Developer版本原價2848現在搶購立享優惠只需2345,立即查看詳情>>
點擊下載MindFusion.Reporting for WinForms最新試用版
該ReportEditor控件可用于創建、打開、保存、預覽和打印MindFusion.Reporting報表。它可以集成在任何WinForms應用程序中,并為其提供編輯報表的能力。這對于全規模的報表應用程序以及較小的應用程序來說是非常有用的,它可以讓最終用戶在可視化的環境中修改現有的報表。例如,托管ReportEditor控件的應用程序可用于允許最終用戶根據其需求調整預先存在的報表模板。
注釋說明
ReportEditor類位于MindFusion.Reporting.Editor.dll集合中。
注意事項
使用報表編輯器的應用程序的最低要求 "目標框架 "是".NET框架4"。
編輯器組件
該編輯器由四個主要組件組成--設計器區域、屬性網格、工具箱和預覽面板。下圖展示了ReportEditor控件的默認外觀。
使用編輯器
用戶體驗
由于ReportEditor控件是基于在Visual Studio中實現MindFusion.Reporting報表設計時支持的相同組件和服務,因此從用戶的角度來看,使用ReportEditor控件創建報表和在Visual Studio中創建報表幾乎是相同的。要了解如何交互式設計報表的基礎知識,請查看報表設計器主題。創建、打開和保存報表
編輯器內顯示的報表實例可以通過報表屬性進行訪問。注意,這個屬性是只讀的。正因為如此,要打開一個現有的報表進行編輯,首先要使用它的SaveToXml方法保存報表,然后通過OpenReport方法在編輯器里面打開報表。直接通過調用Report本身的LoadFromXml打開報表是不可取的。更為一般的是,以任何方式對編輯后的報表進行程序修改都需要謹慎。請查看本主題末尾的 "以編程方式修改已編輯的報表 "部分,了解如何進行修改的提示。
要創建新報表或保存當前報表,分別使用NewReport和SaveReport方法。要檢查當前報表是否在打開或最后保存后被修改,請使用IsReportDirty屬性。每當通過編輯器更改、打開或保存報表時,這個屬性都會自動更新。
命令
編輯器支持各種標準菜單命令,可以通過InvokeCommand方法調用。例如,要對齊當前在編輯器中選擇的報表項的左側,使用以下代碼調用標準命令AlignLeft。
C#
reportEditor.InvokeCommand(StandardCommands.Align);
Visual Basic
reportEditor.InvokeCommand(StandardCommands.Align)另外,還可以通過MenuCommandService調用命令,該命令可以通過編輯器的GetService方法獲得。
下表列出了編輯器支持的大部分標準菜單命令。
此外,編輯器還支持MenuCommands類的自定義菜單命令。
將UI元素與命令關聯起來
報告編輯器提供了監聽支持的命令變化的方法。當應用程序中的一個UI元素需要與一個特定的命令相關聯時,這很有用。當命令未啟用時,該元素需要自動灰化(或禁用)以表明這一點。例如,當Undo不可用時,應用程序中觸發Undo命令的按鈕應該被禁用。
要監聽命令的變化,將一個實現ICommandListener接口的類的實例(以及相應命令的ID)傳遞給RegisterCommandListener方法。當具有指定ID的命令的狀態發生變化時,通過其UpdateState方法通知指定的監聽器。
ICommandListener接口的實現取決于需要更新的用戶元素的類型。例如,讓我們檢查一個應用程序,它提供了一個ToolStripButton組件來觸發各種命令。下面是這個應用程序的ICommandListener接口的一個示例實現。
C#
private class ToolStripItemCommandListener : ICommandListener
{
public CommandListener(ToolStripItem item)
{
this.item = item;
}
public void UpdateState(bool enabled, bool visible)
{
item.Enabled = enabled;
item.Visible = visible;
}
private ToolStripItem item;
}
Visual Basic
Private Class CommandListener Implements ICommandListener Public Sub New(item As ToolStripItem) Me.item = item End Sub Public Sub UpdateState(enabled As Boolean, visible As Boolean) Implements ICommandListener.UpdateState item.Enabled = enabled item.Visible = visible End Sub Private item As ToolStripItem End Class讓我們假設 undoButton 是應用程序中現有的 ToolStripButton,它應該觸發 Undo 命令。那么下面的代碼將把 undoButton 與 Undo 命令關聯起來。
reportEditor.RegisterCommandListener(StandardCommands.Undo, new CommandListener(undoButton));Visual Basic
reportEditor.RegisterCommandListener(StandardCommands.Undo, New CommandListener(undoButton))現在,當Undo可用時,按鈕將被啟用,當Undo不可用時,按鈕將被禁用。
注冊數據源
數據源代表報表元素可以綁定的數據對象。任何對象都可以作為數據源,包括DataSet、業務對象的數組、自定義對象等。數據源可以通過AddDataSource重載之一添加到編輯器中。新添加的數據源會以組件的形式出現在設計器區域底部的小托盤窗口中,隨后可以在屬性網格內選擇數據源作為DataRange和Chart報表項的數據源。在編輯器中添加數據源并不會導致編輯的報表變臟,也不會以任何方式改變報表,直到數據源真正被選為報表項的DataSource。創建新報表(通過NewReport)或關閉當前報表(通過CloseReport)不會導致注冊的數據源消失。
當一個數據源被添加到編輯器中時,它將與一個唯一的名稱相關聯。該名稱在保存報表時被序列化,并在反序列化過程中用于識別項目所綁定的數據源。數據源的名稱可以通過使用 AddDataSource overload 顯式指定,也可以通過使用 AddDataSource overload 由編輯器自動生成。這兩個重載都會返回一個IComponent對象,它是編輯器內部代表數據源的組件。在編輯器自動生成數據源名稱的情況下,這個名稱可以通過返回對象的IComponent.Site.Name屬性獲得。
DataRange對象可以從注冊的數據源中自動創建。要做到這一點,請在設計器區域內的頁面上點擊右鍵,并在上下文菜單中選擇 "Create DataRange from Data Source... "項。這將顯示一個對話框,里面有所有注冊的數據源和它們的字段或屬性。用戶可以選擇要包含在生成的DataRange中的成員。
以編程方式修改報表
編輯后的報表不應直接修改。更具體地說,不應該通過構造函數創建新的項目并添加到報表或項目容器中,也不應該通過屬性分配刪除或修改現有項目。直接修改不會被撤銷引擎跟蹤,不會自動更新設計器表面和UI元素,而且往往會引起異常。例如,不建議進行以下修改。
C#
reportEditor.Report.Pages.Add(new Page())。 reportEditor.Report.Pages[0].Items[0].Location = new Location(10, 10); reportEditor.Report.Pages.RemoveAt(0);Visual Basic
reportEditor.Report.Pages.Add(New Page()) reportEditor.Report.Pages[0].Items[0].Location = New Location(10, 10) reportEditor.Report.Pages.RemoveAt(0)創建項目
新項目應該通過IDesignerHost服務來創建??梢酝ㄟ^調用GetService方法從編輯器中獲得對該服務的引用。下面的代碼說明了如何創建一個新的頁面。
C#
IDesignerHost designerHost = reportEditor.GetService<IDesignerHost>(); Page newPage = designerHost.CreateComponent(typeof(Page)) as Page.CreateComponent(typeof(Page));Visual Basic
Dim designerHost As IDesignerHost = reportEditor.GetService(Of IDesignerHost)() Dim newPage As Page = CType(designerHost.CreateComponent(GetType(Page)), Page)添加項目
向容器中添加新的項目應該通過調用IComponentChangeService的OnComponentChanging和OnComponentChanged方法來包裝。這個服務的引用也可以通過GetService方法獲得。下面的代碼說明了如何將之前創建的頁面實例添加到報表中。
C#
IComponentChangeService componentChange = reportEditor.GetService<IComponentChangeService>(); componentChange.OnComponentChanging(reportEditor.Report, null); reportEditor.Report.Pages.Add(newPage); componentChange.OnComponentChanged(reportEditor.Report, null, null, null);Visual Basic
Dim componentChange As IComponentChangeService = reportEditor.GetService(Of IComponentChangeService)() componentChange.OnComponentChanging(reportEditor.Report, Nothing) reportEditor.Report.Pages.Add(newPage) componentChange.OnComponentChanged(reportEditor.Report, Nothing, Nothing, Nothing);修改項目
PropertyDescriptor locationProperty = TypeDescriptor.GetProperties(item)["Location"]; locationProperty.SetValue(item, new Location(0, 0));Visual Basic
Dim locationProperty As PropertyDescriptor = TypeDescriptor.GetProperties(item)("Location") locationProperty.SetValue(item, New Location(0, 0))移除項目
IDesignerHost designerHost = reportEditor.GetService<IDesignerHost>()。 IComponentChangeService componentChange = reportEditor.GetService<IComponentChangeService>(); componentChange.OnComponentChanging(reportEditor.Report, null); page removedPage = reportEditor.Report.Pages[0]; reportEditor.Report.Pages.RemoveAt(0); componentChange.OnComponentChanged(reportEditor.Report, null, null, null); designerHost.DestroyComponent(removementPage);Visual Basic
Dim designerHost As IDesignerHost = reportEditor.GetService(Of IDesignerHost)() Dim componentChange As IComponentChangeService = reportEditor.GetService(Of IComponentChangeService)() componentChange.OnComponentChanging(reportEditor.Report, Nothing) Dim removedPage as Page = reportEditor.Report.Pages(0) reportEditor.Report.Pages.RemoveAt(0) componentChange.OnComponentChanged(reportEditor.Report, Nothing, Nothing, Nothing) designerHost.DestroyComponent(removementPage)備注
使用事務
對報告的每一次修改都會在撤銷歷史記錄中產生一條撤銷記錄。由于各種間接的修改,如新創建對象的屬性初始化,有些修改甚至會產生不止一條記錄。為了將多個修改打包成一條撤銷記錄,可以在一個事務中執行。下面的例子是在一個事務內部修改一個現有項目的位置和大小。
C#
IDesignerHost designerHost = reportEditor.GetService<IDesignerHost>(); 使用(DesignerTransaction transaction = designerHost.CreateTransaction("修改項目")) { PropertyDescriptor locationProperty = TypeDescriptor.GetProperties(item)["Location"]; locationProperty.SetValue(item, new Location(0, 0)); PropertyDescriptor sizeProperty = TypeDescriptor.GetProperties(item)["Size"]; sizeProperty.SetValue(item, new Dimension(20, 20)); transaction.Commit(); }Visual Basic
Dim designerHost As IDesignerHost = reportEditor.GetService(Of IDesignerHost)() Using transaction As DesignerTransaction = designerHost.CreateTransaction("Modifying item") Dim locationProperty As PropertyDescriptor = TypeDescriptor.GetProperties(item)("Location") locationProperty.SetValue(item, New Location(0, 0)) Dim sizeProperty As PropertyDescriptor = TypeDescriptor.GetProperties(item)("Size") sizeProperty.SetValue(item, New Dimension(20, 20)) transaction.Commit() End Using也可以嵌套交易。
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自: