【问题标题】:DevExpress XtraReport replace XRPictureBox control on runtimeDevExpress XtraReport 在运行时替换 XRPictureBox 控件
【发布时间】:2015-12-01 16:30:23
【问题描述】:

因此,在我们的报告中,我们在 Detail Band 中有一个 ImageBox,我们希望每页都有不同的图像。
看到在 Detail Band 的 BeforePrint 事件中更改它不起作用(因为每次打印报告只调用一次)我遵循以下方法到目前为止(不起作用):

在报告的页眉带标签的 PrintOnPage 事件中(为了将其称为 per page 并能够在新的 XRPictureBox 中使用 PrintOnPageEventArgs - 我需要 PageIndex ):

private void xrLabel10_PrintOnPage(object sender, PrintOnPageEventArgs e)
{
    this.Detail.Controls.Remove(xrPictureBox1); //Existing PictureBox control
    this.Detail.Controls.Add(PictureBox(e));
}

这叫:

private List<Bitmap> _imageList;

public XRPictureBox PictureBox(PrintOnPageEventArgs e)
{
    XRPictureBox pictureBox = new XRPictureBox();
    var articleService = DependencyResolver.Current.GetService<IArticleService>();
    int width;
    int height;

    pictureBox.Name = "xrPictureBox_" + e.PageIndex;

    pictureBox.BackColor = Color.Transparent;
    pictureBox.Dpi = 254F;
    pictureBox.LocationFloat = new DevExpress.Utils.PointFloat(108.4792F, 71.4375F);
    pictureBox.Name = "xrPictureBox_" + e.PageIndex;
    pictureBox.SizeF = new SizeF(950F, 1225F);
    pictureBox.Sizing = ImageSizeMode.Squeeze;
    pictureBox.StylePriority.UseBackColor = false;

    if (ReportUnit == ReportUnit.HundredthsOfAnInch)
    {
        width = (int)(GraphicsUnitConverter.Convert(xrPictureBox1.Size.Width, GraphicsUnit.Inch, GraphicsUnit.Pixel) / 100);
        height = (int)(GraphicsUnitConverter.Convert(xrPictureBox1.Size.Height, GraphicsUnit.Inch, GraphicsUnit.Pixel) / 100);
    }
    else
    {
        width = (int)(GraphicsUnitConverter.Convert(xrPictureBox1.Size.Width, GraphicsUnit.Millimeter, GraphicsUnit.Pixel) / 10);
        height = (int)(GraphicsUnitConverter.Convert(xrPictureBox1.Size.Height, GraphicsUnit.Millimeter, GraphicsUnit.Pixel) / 10);
    }

    if (_imageList == null)
    {
        _imageList = articleService .GetListOfImages((int)articleId.Value, width, height, PageColor); //this gets a List<Bitmap>
    }

    if (_imageList[e.PageIndex] == null)
        return null;
    pictureBox.Image = _imageList[e.PageIndex];

    return pictureBox;
}

所以基本上我的想法是用新的 XRPictureBox 替换现有的 Control 和新的图像。但它只是没有出现在报告中,即使在调试时我看到代码正在运行并为正确的页面检索正确的图像。

编辑:nempoBu4 的回答通常是正确的,但不幸的是,我未能澄清一个额外的问题,这使得它不适合我的情况: 该报表在 PictureBox 旁边还有一个子报表,该子报表可以扩展到多个页面。我们希望 PictureBox 在每个页面中呈现不同的图像,并且它们不会触发 PictureBox 的 PrintOnPage 事件。我会尽快用我们找到的解决方法添加答案:)

【问题讨论】:

    标签: c# asp.net devexpress xtrareport


    【解决方案1】:

    图片框

    您可以使用PictureBox 本身的PrintOnPage 事件。
    示例如下:

    var source = new List<Tuple<int, string>>();
    
    for (int index = 0; index < 100; index++)
        source.Add(new Tuple<int, string>(index, "Name" + index));
    
    var pictureBox = new XRPictureBox();
    pictureBox.PrintOnPage += (sender, e) =>
    {
        if (_imageList[e.PageIndex] == null)
            return;
    
        pictureBox.Image = _imageList[e.PageIndex];
    };
    
    var labelItem1 = new XRLabel();
    labelItem1.DataBindings.Add("Text", null, "Item1");
    labelItem1.LeftF = 100;
    
    var labelItem2 = new XRLabel();
    labelItem2.DataBindings.Add("Text", null, "Item2");
    labelItem2.LeftF = 200;
    
    var detail = new DetailBand();
    detail.Controls.AddRange(new XRControl[] { pictureBox, labelItem1, labelItem2 });
    
    var report = new XtraReport();
    report.Bands.Add(detail);
    report.DataSource = source;
    
    report.ShowRibbonPreview();
    

    示例结果:

    水印

    如果您希望每页只有一张图片,则可以使用水印。
    这是一个例子:

    var source = new List<Tuple<int, string>>();
    
    for (int index = 0; index < 100; index++)
        source.Add(new Tuple<int, string>(index, "Name" + index));
    
    var labelItem1 = new XRLabel();
    labelItem1.DataBindings.Add("Text", null, "Item1");
    labelItem1.LeftF = 100;
    
    var labelItem2 = new XRLabel();
    labelItem2.DataBindings.Add("Text", null, "Item2");
    labelItem2.LeftF = 200;
    
    var detail = new DetailBand();
    detail.Controls.AddRange(new XRControl[] { labelItem1, labelItem2 });
    
    var report = new XtraReport();
    report.Bands.Add(detail);
    report.DataSource = source;
    
    report.CreateDocument();
    
    foreach (Page page in report.Pages)
        if (_imageList[page.Index] != null)
        {
            var watermark = new Watermark();
            watermark.Image = _imageList[page.Index];
    
            page.AssignWatermark(watermark);
        }
    
    report.ShowRibbonPreview();
    

    这是结果:

    【讨论】:

    • 你的回答是正确的,这会起作用,但不幸的是我的问题有点不完整。问题是该报告还包含一个子报告,在 PictureBox 旁边,它可以扩展到多个页面。因此不幸的是,PrintOnPage 事件只为 PictureBox 调用一次,而我们可以在它旁边有 3-4 页的子报表,导致报表仅在第一页上有一个图像。我们为我们的案例找到了一种解决方法,我最终会尝试发布。
    • @DimitrisG。据我了解,您希望每页有一张图片。如果是这样,那么您可以使用水印。我已经更新了我的答案。
    猜你喜欢
    • 1970-01-01
    • 2018-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多