也来一篇关于Infragistics WPF Report的使用教程 (一)

前言

Infragistics Report是一款比较灵活的报表控件, 比微软的rdlc控件至少在页面打印上, 页面的控制比较好调整.

这里使用的是Infragistics Ultimate  v14.1 试用版

开发工具是Visual Studio 2013 Framework 4.0 WPF Windows应用程序.

添加报表

将XamReportViewer从左侧的工具栏中拖到右侧的窗体中.

<ig:XamReportViewer HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
</ig:XamReportViewer>

系统会自动的添加引用, 记住这些引用,发布的时候, 把这些引用一并打包. 这样在客户端运行时就不会出现问题了.

1. 先定义一个Person的类, 实现INofifyPropertyChanged接口.

   public class Person:INotifyPropertyChanged
    {
        #region  Implement of INotifyProeprtyChanged.
        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = this.PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion

        private string _name;
        private int _age;
        private byte[] _profilePhoto;
        public string Name
        {
            get { return _name; }
            set { _name = value;
                OnPropertyChanged("Name");
            }
        }

        public int Age
        {
            get { return _age; }
            set { _age = value; OnPropertyChanged("Age"); }
        }

        public byte[] ProfilePhoto
        {
            get { return _profilePhoto; }
            set { _profilePhoto = value; OnPropertyChanged("ProfilePhoto"); }
        }
    }

2. 我们再定义一个MainWindowViewModel, 用于关联MainWindow.xaml的DataContext.

在MainWindow.xaml 中我们要New出一个 MainWindowViewModel的实体, 并对实体中的属性赋值. 这样才可以将对应的参数绑定到报表中. 即MVVM模式.

public class MainWindowViewModel : INotifyPropertyChanged
    {
        #region  Implement of INotifyProeprtyChanged.

        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = this.PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion

        private ObservableCollection<Person> _personCollection;
        private DateTime _printDateTime;

        public ObservableCollection<Person> PersonCollection
        {
            get { return _personCollection; }
            set
            {
                _personCollection = value;
                OnPropertyChanged("PersonCollection");
            }
        }

        public DateTime PrintDateTime
        {
            get { return _printDateTime; }
            set
            {
                _printDateTime = value;
                OnPropertyChanged("PrintDateTime");
            }
        }
    }

创建报表

添加数据源

1. 创建报表, 安装Infragistics之后, 新添加项目的时候, 会有一项infragistics, 按图所示, 添加报表.

2. 创建数据源

通过Report Data Explorer工具栏, 右击, 选择 DataSource - Add new Data Source,

在后面弹出的窗口中选择"Object Data Source", 点击下一步,

选择我们刚刚定义好的MainWindowViewModel.cs, 这里需要注意一下, 在创建编辑好MainViewModel.cs之后, 需要编译一下, 添加数据源的时候才能显示出来. 否则是不会显示的.

添加参数

添加参数的相对简单, 从左侧的Report Data Explorer中右击Parameter, 选择"Add Static Value List Parameter", 输入参数名称即可.

添加完成之后,  点击OK即可, 然后将数据源, 参数直接拖到右侧的报表区域中.

绑定参数

绑定的参数之后的代码

        <ig:XamReportViewer HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
            <ig:XamReportViewer.RenderSettings>
                <ig:ClientRenderSettings  DefinitionUri="/InfragisticsReportSample;component/Person.igr">
                    <ig:ClientRenderSettings.DataSources>
                        <ig:DataSource TargetDataSource="Person" ItemsSource="{Binding PersonCollection}"/>
                    </ig:ClientRenderSettings.DataSources>
                </ig:ClientRenderSettings>
            </ig:XamReportViewer.RenderSettings>
            <ig:XamReportViewer.Parameters>
                <ig:Parameter ParameterName="PrintDate" ParameterValue="{Binding PrintDateTime}"/>
            </ig:XamReportViewer.Parameters>
        </ig:XamReportViewer>

TargetDataSource = "Person" 指的是绑定的Collection的类型为Person

ItemsSource = "{Binding PersonCollection}" 指的是绑定数据源为PersonCollection

Parametername = "PrintDate" 是指我们在报表中创建的参数名称为PrintDate,  ParameterValue = {Binding PrintDateTime} 是指绑定PrintDateTime的属性.

添加数据

在创建出MainWindowViewModel的时候, 指定数据源, 就可以显示出报表了.

       public MainWindow()
        {
            InitializeComponent();

            MainWindowViewModel model = new MainWindowViewModel();

            model.PersonCollection = new ObservableCollection<Person>();
            model.PrintDateTime = DateTime.Now;

            Person p = new Person();
            p.Name = "哆拉A梦";
            p.Age = 99;
            p.ProfilePhoto = GetByteImage("doraemon.jpg");
            model.PersonCollection.Add(p);

            p = new Person();
            p.Name = "阿拉蕾";
            p.Age = 100;
            p.ProfilePhoto = GetByteImage("arale.jpg");
            model.PersonCollection.Add(p);

            this.DataContext = model;
        }

        public byte[] GetByteImage(string imagepath)
        {
            FileStream fs = new FileStream(imagepath, FileMode.Open);
            byte[] byData = new byte[fs.Length];
            fs.Read(byData, 0, byData.Length);
            fs.Close();
            return byData;
        }
    }

最后的报表结果

也来一篇关于Infragistics WPF Report的使用教程 (一)

时间: 2024-08-23 16:39:59

也来一篇关于Infragistics WPF Report的使用教程 (一)的相关文章

WPF 精修篇 Winform 嵌入WPF控件

原文:WPF 精修篇 Winform 嵌入WPF控件 首先 创建WPF控件库 这样就有了一个WPF界面 在wpf中增加界面等 在winform中增加WPFDLL 重新生成解决方案 在左侧工具栏 出现WPF 控件 拖到窗体 效果 原文地址:https://www.cnblogs.com/lonelyxmas/p/12075801.html

几篇不错的关于蜘蛛程序的教程

几篇不错的关于蜘蛛程序的教程,转录一下: pyspider 爬虫教程(一):HTML 和 CSS 选择器 pyspider 爬虫教程(二):AJAX 和 HTTP pyspider 爬虫教程(三):使用 PhantomJS 渲染带 JS 的页面 ?

Infragistics WPF 应用主题到所有控件

引用 Infragistics.Themes.MetroDarkTheme.dll (本文以MetroDarkTheme为例) 并在 App.xaml 中添加: <Application x:Class="MetroDarkThemeTest.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.

“洋垃圾”复活记:夏普shl25刷机救砖实战指南(也许是网上第一篇完整的SHL25的刷机教程)

废话: 两个月前在网上发现一篇二手手机购买使用评测,手机外观漂亮,机器参数牛逼,价格白菜价. 很久没折腾了,瞬间手痒想买来做备用机,立马某宝下单.快递很快,第二天就到了.开箱很新, 不仔细看看不出是二手机,九成新的,赚大了,接下来就看人品能用多久了. 声明:这不是广告,某宝没卖下架啦o(∩_∩)o 哈哈,教程仅供参考,刷机有风险的哟 日媒解析: AQUOS SERIE SHL25拥有最先进的CPU.高画质的照相机.低耗能的IGZO液晶屏,同时具有防水.防尘.full segment等功能,最大传

Pentaho Report Designer 入门教程(三)

采用Pentaho Report Designer5.1版本,也是最新的版本. 一.       安装和介绍 介绍部分内容略,首先安装jdk,并配置java相关环境变量,下载pentaho report并解压,直接运行即可. 二.       第一个示例 三.在Swing程序中集成 四.在j2ee程序中集成 ?  新建web项目 ?  编写ant脚本,编译运行项目 <?xml version="1.0"encoding="UTF-8" standalone=&

Grid++Report 数据填充教程

用 Grid++Report的报表设计器应用程序设计一个简单的报表:“机房开发收入总汇表”                  一.定义报表头 1.执行菜单命令“插入”→“报表头” 2.执行菜单命令“插入”→“静态框” 3.设置 StaticBox1 的“文本”属性设为“机房开发收入总汇表”,“文本对齐方式”和 “字体”属性.  二.插入明细网格 1.执行菜单命令“插入”→“明细网格” 三.绑定明细网格数据         Grid++Report 采用 Windows 操作系统自带的 OLE D

Pentaho Report Designer 入门教程(一)

PentahoReport Designer 入门教程 采用Pentaho Report Designer5.1版本,也是最新的版本. 一.       安装和介绍 介绍部分内容略,首先安装jdk,并配置java相关环境变量,下载pentaho report并解压,直接运行即可. 二.       第一个示例 本示例是<Pentaho Reporting 3.5 for Java Developers>第二章中的例子,比较容易上手:在报表中显示11个java库及其大小(最新版本5.1已经不是1

传智播客WPF第三季的基础教程

本来想上传到csdn进行资源共享的,无奈的发现,csdn现在越来越恶心了,竟然不能自己定积分了,也不知道是根据什么来定的资源的价值 反正是没发自定义下载积分了,还是回归博客园的怀抱吧 最近公司在转型到wpf,所以找些资料来学习下,作为传智播客的公开课的受益者,首先想到的就是查一下传智播客有没有视频教程----结果就这到了这个教程 不负有心人啊,找到了免费的资源,现在分享出来. 链接: https://pan.baidu.com/s/1-Y0KnsVcTF76D5drIdPR6A 提取码: dtb

WPF/MVVM模式入门教程(二):实现INotifyPropertyChanged接口

1.创建NotifyPropertyChanged类 我们在common文件夹下创建一个名为NotifyPropertyChanged.cs的类,该类继承INotifyPropertyChanged接口主要用于消息通知,当UI里的值发生改变的时候,能够触发相应的改变. using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Text;