翻譯|使用教程|編輯:楊鵬連|2021-02-20 10:24:42.417|閱讀 328 次
概述:該ReportEditor控件可用于創(chuàng)建、打開(kāi)、保存、預(yù)覽和打印MindFusion.Reporting報(bào)表。它可以集成在任何WinForms應(yīng)用程序中,并為其提供編輯報(bào)表的能力。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門(mén)軟控件火熱銷(xiāo)售中 >>
MindFusion.Reporting for WinForms是一個(gè)原生的Windows Forms編程組件,它為任何.NET應(yīng)用程序提供專(zhuān)業(yè)的報(bào)表功能。該組件完全使用C#語(yǔ)言編寫(xiě),易于使用和集成。它提供您創(chuàng)建一個(gè)完美報(bào)表所需要的一切。MindFusion.Reporting for WinForms現(xiàn)已加入在線訂購(gòu),Standard Single Developer版本原價(jià)2848現(xiàn)在搶購(gòu)立享優(yōu)惠只需2345,立即查看詳情>>
點(diǎn)擊下載MindFusion.Reporting for WinForms最新試用版
該ReportEditor控件可用于創(chuàng)建、打開(kāi)、保存、預(yù)覽和打印MindFusion.Reporting報(bào)表。它可以集成在任何WinForms應(yīng)用程序中,并為其提供編輯報(bào)表的能力。這對(duì)于全規(guī)模的報(bào)表應(yīng)用程序以及較小的應(yīng)用程序來(lái)說(shuō)是非常有用的,它可以讓最終用戶在可視化的環(huán)境中修改現(xiàn)有的報(bào)表。例如,托管ReportEditor控件的應(yīng)用程序可用于允許最終用戶根據(jù)其需求調(diào)整預(yù)先存在的報(bào)表模板。
注釋說(shuō)明
ReportEditor類(lèi)位于MindFusion.Reporting.Editor.dll集合中。
注意事項(xiàng)
使用報(bào)表編輯器的應(yīng)用程序的最低要求 "目標(biāo)框架 "是".NET框架4"。
編輯器組件
該編輯器由四個(gè)主要組件組成--設(shè)計(jì)器區(qū)域、屬性網(wǎng)格、工具箱和預(yù)覽面板。下圖展示了ReportEditor控件的默認(rèn)外觀。
使用編輯器
用戶體驗(yàn)
由于ReportEditor控件是基于在Visual Studio中實(shí)現(xiàn)MindFusion.Reporting報(bào)表設(shè)計(jì)時(shí)支持的相同組件和服務(wù),因此從用戶的角度來(lái)看,使用ReportEditor控件創(chuàng)建報(bào)表和在Visual Studio中創(chuàng)建報(bào)表幾乎是相同的。要了解如何交互式設(shè)計(jì)報(bào)表的基礎(chǔ)知識(shí),請(qǐng)查看報(bào)表設(shè)計(jì)器主題。創(chuàng)建、打開(kāi)和保存報(bào)表
編輯器內(nèi)顯示的報(bào)表實(shí)例可以通過(guò)報(bào)表屬性進(jìn)行訪問(wèn)。注意,這個(gè)屬性是只讀的。正因?yàn)槿绱耍蜷_(kāi)一個(gè)現(xiàn)有的報(bào)表進(jìn)行編輯,首先要使用它的SaveToXml方法保存報(bào)表,然后通過(guò)OpenReport方法在編輯器里面打開(kāi)報(bào)表。直接通過(guò)調(diào)用Report本身的LoadFromXml打開(kāi)報(bào)表是不可取的。更為一般的是,以任何方式對(duì)編輯后的報(bào)表進(jìn)行程序修改都需要謹(jǐn)慎。請(qǐng)查看本主題末尾的 "以編程方式修改已編輯的報(bào)表 "部分,了解如何進(jìn)行修改的提示。
要?jiǎng)?chuàng)建新報(bào)表或保存當(dāng)前報(bào)表,分別使用NewReport和SaveReport方法。要檢查當(dāng)前報(bào)表是否在打開(kāi)或最后保存后被修改,請(qǐng)使用IsReportDirty屬性。每當(dāng)通過(guò)編輯器更改、打開(kāi)或保存報(bào)表時(shí),這個(gè)屬性都會(huì)自動(dòng)更新。
命令
編輯器支持各種標(biāo)準(zhǔn)菜單命令,可以通過(guò)InvokeCommand方法調(diào)用。例如,要對(duì)齊當(dāng)前在編輯器中選擇的報(bào)表項(xiàng)的左側(cè),使用以下代碼調(diào)用標(biāo)準(zhǔn)命令A(yù)lignLeft。
C#
reportEditor.InvokeCommand(StandardCommands.Align);
Visual Basic
reportEditor.InvokeCommand(StandardCommands.Align)另外,還可以通過(guò)MenuCommandService調(diào)用命令,該命令可以通過(guò)編輯器的GetService方法獲得。
下表列出了編輯器支持的大部分標(biāo)準(zhǔn)菜單命令。
此外,編輯器還支持MenuCommands類(lèi)的自定義菜單命令。
將UI元素與命令關(guān)聯(lián)起來(lái)
報(bào)告編輯器提供了監(jiān)聽(tīng)支持的命令變化的方法。當(dāng)應(yīng)用程序中的一個(gè)UI元素需要與一個(gè)特定的命令相關(guān)聯(lián)時(shí),這很有用。當(dāng)命令未啟用時(shí),該元素需要自動(dòng)灰化(或禁用)以表明這一點(diǎn)。例如,當(dāng)Undo不可用時(shí),應(yīng)用程序中觸發(fā)Undo命令的按鈕應(yīng)該被禁用。
要監(jiān)聽(tīng)命令的變化,將一個(gè)實(shí)現(xiàn)ICommandListener接口的類(lèi)的實(shí)例(以及相應(yīng)命令的ID)傳遞給RegisterCommandListener方法。當(dāng)具有指定ID的命令的狀態(tài)發(fā)生變化時(shí),通過(guò)其UpdateState方法通知指定的監(jiān)聽(tīng)器。
ICommandListener接口的實(shí)現(xiàn)取決于需要更新的用戶元素的類(lèi)型。例如,讓我們檢查一個(gè)應(yīng)用程序,它提供了一個(gè)ToolStripButton組件來(lái)觸發(fā)各種命令。下面是這個(gè)應(yīng)用程序的ICommandListener接口的一個(gè)示例實(shí)現(xiàn)。
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讓我們假設(shè) undoButton 是應(yīng)用程序中現(xiàn)有的 ToolStripButton,它應(yīng)該觸發(fā) Undo 命令。那么下面的代碼將把 undoButton 與 Undo 命令關(guān)聯(lián)起來(lái)。
reportEditor.RegisterCommandListener(StandardCommands.Undo, new CommandListener(undoButton));Visual Basic
reportEditor.RegisterCommandListener(StandardCommands.Undo, New CommandListener(undoButton))現(xiàn)在,當(dāng)Undo可用時(shí),按鈕將被啟用,當(dāng)Undo不可用時(shí),按鈕將被禁用。
注冊(cè)數(shù)據(jù)源
數(shù)據(jù)源代表報(bào)表元素可以綁定的數(shù)據(jù)對(duì)象。任何對(duì)象都可以作為數(shù)據(jù)源,包括DataSet、業(yè)務(wù)對(duì)象的數(shù)組、自定義對(duì)象等。數(shù)據(jù)源可以通過(guò)AddDataSource重載之一添加到編輯器中。新添加的數(shù)據(jù)源會(huì)以組件的形式出現(xiàn)在設(shè)計(jì)器區(qū)域底部的小托盤(pán)窗口中,隨后可以在屬性網(wǎng)格內(nèi)選擇數(shù)據(jù)源作為DataRange和Chart報(bào)表項(xiàng)的數(shù)據(jù)源。在編輯器中添加數(shù)據(jù)源并不會(huì)導(dǎo)致編輯的報(bào)表變臟,也不會(huì)以任何方式改變報(bào)表,直到數(shù)據(jù)源真正被選為報(bào)表項(xiàng)的DataSource。創(chuàng)建新報(bào)表(通過(guò)NewReport)或關(guān)閉當(dāng)前報(bào)表(通過(guò)CloseReport)不會(huì)導(dǎo)致注冊(cè)的數(shù)據(jù)源消失。
當(dāng)一個(gè)數(shù)據(jù)源被添加到編輯器中時(shí),它將與一個(gè)唯一的名稱(chēng)相關(guān)聯(lián)。該名稱(chēng)在保存報(bào)表時(shí)被序列化,并在反序列化過(guò)程中用于識(shí)別項(xiàng)目所綁定的數(shù)據(jù)源。數(shù)據(jù)源的名稱(chēng)可以通過(guò)使用 AddDataSource overload 顯式指定,也可以通過(guò)使用 AddDataSource overload 由編輯器自動(dòng)生成。這兩個(gè)重載都會(huì)返回一個(gè)IComponent對(duì)象,它是編輯器內(nèi)部代表數(shù)據(jù)源的組件。在編輯器自動(dòng)生成數(shù)據(jù)源名稱(chēng)的情況下,這個(gè)名稱(chēng)可以通過(guò)返回對(duì)象的IComponent.Site.Name屬性獲得。
DataRange對(duì)象可以從注冊(cè)的數(shù)據(jù)源中自動(dòng)創(chuàng)建。要做到這一點(diǎn),請(qǐng)?jiān)谠O(shè)計(jì)器區(qū)域內(nèi)的頁(yè)面上點(diǎn)擊右鍵,并在上下文菜單中選擇 "Create DataRange from Data Source... "項(xiàng)。這將顯示一個(gè)對(duì)話框,里面有所有注冊(cè)的數(shù)據(jù)源和它們的字段或?qū)傩浴S脩艨梢赃x擇要包含在生成的DataRange中的成員。
以編程方式修改報(bào)表
編輯后的報(bào)表不應(yīng)直接修改。更具體地說(shuō),不應(yīng)該通過(guò)構(gòu)造函數(shù)創(chuàng)建新的項(xiàng)目并添加到報(bào)表或項(xiàng)目容器中,也不應(yīng)該通過(guò)屬性分配刪除或修改現(xiàn)有項(xiàng)目。直接修改不會(huì)被撤銷(xiāo)引擎跟蹤,不會(huì)自動(dòng)更新設(shè)計(jì)器表面和UI元素,而且往往會(huì)引起異常。例如,不建議進(jìn)行以下修改。
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)創(chuàng)建項(xiàng)目
新項(xiàng)目應(yīng)該通過(guò)IDesignerHost服務(wù)來(lái)創(chuàng)建。可以通過(guò)調(diào)用GetService方法從編輯器中獲得對(duì)該服務(wù)的引用。下面的代碼說(shuō)明了如何創(chuàng)建一個(gè)新的頁(yè)面。
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)添加項(xiàng)目
向容器中添加新的項(xiàng)目應(yīng)該通過(guò)調(diào)用IComponentChangeService的OnComponentChanging和OnComponentChanged方法來(lái)包裝。這個(gè)服務(wù)的引用也可以通過(guò)GetService方法獲得。下面的代碼說(shuō)明了如何將之前創(chuàng)建的頁(yè)面實(shí)例添加到報(bào)表中。
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);修改項(xiàng)目
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))移除項(xiàng)目
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)備注
使用事務(wù)
對(duì)報(bào)告的每一次修改都會(huì)在撤銷(xiāo)歷史記錄中產(chǎn)生一條撤銷(xiāo)記錄。由于各種間接的修改,如新創(chuàng)建對(duì)象的屬性初始化,有些修改甚至?xí)a(chǎn)生不止一條記錄。為了將多個(gè)修改打包成一條撤銷(xiāo)記錄,可以在一個(gè)事務(wù)中執(zhí)行。下面的例子是在一個(gè)事務(wù)內(nèi)部修改一個(gè)現(xiàn)有項(xiàng)目的位置和大小。
C#
IDesignerHost designerHost = reportEditor.GetService<IDesignerHost>(); 使用(DesignerTransaction transaction = designerHost.CreateTransaction("修改項(xiàng)目")) { 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也可以嵌套交易。
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自: