MindFusion.Reporting for WinForms是一个原生的Windows Forms编程组件,它为任何.NET应用程序提供专业的 表功能。该组件完全使用C#语言编写,易于使用和集成。它提供您创建一个完美 表所需要的一切。MindFusion.Reporting for WinForms现已加入在线订购,Standard Single Developer版本原价2848现在抢购立享优惠只需2345,立即查看详情>>

MindFusion.Reporting for WinForms最新试用版

用户体验

由于ReportEditor控件是基于在Visual Studio中实现MindFusion.Reporting 表设计时支持的相同组件和服务,因此从用户的角度来看,使用ReportEditor控件创建 表和在Visual Studio中创建 表几乎是相同的。要了解如何交互式设计 表的基础知识,请查看 表设计器主题。创建、打开和保存 表

命令

reportEditor.InvokeCommand(StandardCommands.Align);

Visual Basic

reportEditor.InvokeCommand(StandardCommands.Align)

将UI元素与命令关联起来

要监听命令的变化,将一个实现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 ToolStripItemEnd Class

让我们假设 undoButton 是应用程序中现有的 ToolStripButton,它应该触发 Undo 命令。那么下面的代码将把 undoButton 与 Undo 命令关联起来。
C#

reportEditor.RegisterCommandListener(StandardCommands.Undo, new CommandListener(undoButton));

Visual Basic

reportEditor.RegisterCommandListener(StandardCommands.Undo, New CommandListener(undoButton))

现在,当Undo可用时,按钮将被启用,当Undo不可用时,按钮将被禁用。

注册数据源

DataRange对象可以从注册的数据源中自动创建。要做到这一点,请在设计器区域内的页面上点击右键,并在上下文菜单中选择 “Create DataRange from Data Source… “项。这将显示一个对话框,里面有所有注册的数据源和它们的字段或属性。用户可以选择要包含在生成的DataRange中的成员。

以编程方式修改 表

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 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);

修改项目
可以通过在OnComponentChanging/OnComponentChanged块中封装属性分配来修改项目,类似于添加项目的方式。另外,也可以通过PropertyDescriptor对象来修改项目。下面的代码演示了如何使用第二种方法来改变现有项的位置。
C#

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

也可以嵌套交易。


想要购买该产品正版授权请点击【商城购买】,想了解更多产品信息请点击【咨询在线客服】

标签:

声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2021年1月17日
下一篇 2021年1月17日

相关推荐

发表回复

登录后才能评论