标签位于XAMDataGrid的末尾

时间:2016-01-18 17:51:06

标签: c# wpf xaml infragistics xamdatagrid

我有一个XAMDataGrid,用于通过以下代码填充报告。

其中EGMList是模型。enter image description here

Reports.Xaml

 <dataPresenter:XamDataGrid x:Name="datagdStatus" DataSource="{Binding EgmList}" DockPanel.Dock="Top"
                          HorizontalContentAlignment="Stretch" HorizontalAlignment="Stretch"  GroupByAreaLocation="None" FontFamily="Arial" FontSize="11" Visibility="Hidden">
                    <dataPresenter:XamDataGrid.FieldLayoutSettings>
                        <dataPresenter:FieldLayoutSettings RecordSelectorLocation="None" AllowAddNew="False" AllowDelete="False"  AutoGenerateFields="False" />
                    </dataPresenter:XamDataGrid.FieldLayoutSettings>
                    <dataPresenter:XamDataGrid.FieldLayouts >
                        <dataPresenter:FieldLayout >
                            <dataPresenter:FieldLayout.Fields>
                                <dataPresenter:Field Name="Id" Label="{x:Static p:resource.tab_report_machineid}" Width="1.8*" Visibility="Collapsed">
                                    <dataPresenter:Field.Settings>
                                        <dataPresenter:FieldSettings AllowEdit="False" LabelTextAlignment="Center" EditorStyle="{StaticResource TextEditor}" LabelTextWrapping="Wrap"  />
                                    </dataPresenter:Field.Settings>
                                </dataPresenter:Field>
                                <dataPresenter:Field Name="MacAddress" Label= "{x:Static p:resource.UID}" Width="4*">
                                    <dataPresenter:Field.Settings>
                                        <dataPresenter:FieldSettings AllowEdit="False" LabelTextAlignment="Left" LabelTextWrapping="Wrap" />
                                    </dataPresenter:Field.Settings>
                                </dataPresenter:Field>
                                <dataPresenter:Field Name="HouseNumber" Label= "{x:Static p:resource.tab_chn_houseNumber_Title}" Width="3.3*" >
                                    <dataPresenter:Field.Settings>
                                        <dataPresenter:FieldSettings AllowEdit="False" LabelTextAlignment="Left" />
                                    </dataPresenter:Field.Settings>
                                </dataPresenter:Field>
                                <dataPresenter:Field Name="Type" Label="{x:Static p:resource.tab_report_type}" Width="2.5*" >
                                    <dataPresenter:Field.Settings>
                                        <dataPresenter:FieldSettings AllowEdit="False" LabelTextAlignment="Left" LabelTextWrapping="Wrap" />
                                    </dataPresenter:Field.Settings>
                                </dataPresenter:Field>
                                <dataPresenter:Field Name="Status" Label="{x:Static p:resource.tab_redeem_status}" Width="5*" >
                                    <dataPresenter:Field.Settings>
                                        <dataPresenter:FieldSettings AllowEdit="False" LabelTextAlignment="Left" />
                                    </dataPresenter:Field.Settings>
                                </dataPresenter:Field>
                           </dataPresenter:FieldLayout.Fields>

                        </dataPresenter:FieldLayout>

                    </dataPresenter:XamDataGrid.FieldLayouts>

                </dataPresenter:XamDataGrid>
            </DockPanel>

Reports.Xaml.cs

  private void btnAllStatusPrint_Click(object sender, RoutedEventArgs e)
    {
        if (RedeemVoucherViewModel.EgmList.Count != 0)
        {
            Report reportObj = new Report();
            // set scale mode
            reportObj.ReportSettings.HorizontalPaginationMode = HorizontalPaginationMode.Scale;
            reportObj.PageHeaderTemplate = this.Resources["PagePresenterHeaderTemplate_small"] as DataTemplate;
            //  reportObj.PageContentTemplate = this.TryFindResource("basicContentTemplate") as DataTemplate;
            string siteControllerName = RedeemVoucherView.getSiteControllerName();
           // reportObj.PageFooter = "Total Machines :23";
            reportObj.PageHeader = string.Format("{0} {2} {1}", siteControllerName, DateTime.Now.ToString("dd/MM/yyyy"), Environment.NewLine);
            reportObj.ReportSettings.PageSize = new Size(3.13, 6);
            // create section and add it to report's section collection
            reportObj.ReportSettings.Margin = new Thickness(0, 0, 0, 0);
            EmbeddedVisualReportSection section = new EmbeddedVisualReportSection(datagdStatus);
            reportObj.Sections.Add(section);
            reportObj.Print(true, false);
            rteventLogger.WriteLog(TraceEventType.Information, "All Status report is printed", WMSTrace.ScopeTypes.Internal);
        }
        else
        {
            MessageBoxHelper.Show("No content available to print", MessageBoxImage.Information);
        }
    }

报告将打印在上述函数调用上,并在报表网格中填充EGMList的内容。

我希望报告末尾有一个摘要标签,其中包含记录/ EGM的数量

尝试在 PageFooter 中添加它,但没有运气,因为它在所有页面中打印。

还尝试在代码后面添加一个不同的部分,但同时也显示一起使用不同的页面。

对此有何建议?

1 个答案:

答案 0 :(得分:0)

不幸的是,这不是一件容易的事。

首先我想我只需要为FooterTemplate定义一个新的Report,然后根据它是否在最后一页上隐藏或显示页脚。

不幸的是,生成报告的方式是您不知道总共会生成多少页面,因此在数据模板中您不知道是否在模板最后一页

来源:Source1

Source2:Source2

解决方法

我只能为您提供解决方法。如果您不介意两次生成报告,那么它是可行的。 首先我们生成它,以便获得总页数,我们可以使用它来设置FooterTemplate

private void btnAllStatusPrint_Click(object sender, RoutedEventArgs e)
{
    if (RedeemVoucherViewModel.EgmList.Count != 0)
    {
        Report reportObj = new Report();
        // set scale mode
        reportObj.ReportSettings.HorizontalPaginationMode = HorizontalPaginationMode.Scale;
        reportObj.PageHeaderTemplate = this.Resources["PagePresenterHeaderTemplate_small"] as DataTemplate;
        string siteControllerName = RedeemVoucherView.getSiteControllerName();

        reportObj.PageFooter = string.Format("Total Machines: {0}", MyXamDataGrid.DataItems.Count);

        reportObj.PageHeader = string.Format("{0} {2} {1}", siteControllerName, DateTime.Now.ToString("dd/MM/yyyy"), Environment.NewLine);
        reportObj.ReportSettings.PageSize = new Size(3.13, 6);
        // create section and add it to report's section collection
        reportObj.ReportSettings.Margin = new Thickness(0, 0, 0, 0);
        EmbeddedVisualReportSection section = new EmbeddedVisualReportSection(datagdStatus);
        reportObj.Sections.Add(section);

        var xamReportPreviewToGetTotalPageNumbers = new XamReportPreview();
        xamReportPreviewToGetTotalPageNumbers.GeneratePreview(reportObj, false, false);
        reportObj.PageFooterTemplate = GetDataTemplateForTotalPageNumbers(reportObj.PhysicalPageNumber);

        reportObj.Print(true, false);
        rteventLogger.WriteLog(TraceEventType.Information, "All Status report is printed", WMSTrace.ScopeTypes.Internal);
    }
    else
    {
        MessageBoxHelper.Show("No content available to print", MessageBoxImage.Information);
    }
}

GetDataTemplateForTotalPageNumbers如下:

private DataTemplate GetDataTemplateForTotalPageNumbers(int totalPages)
{
    var dataTemplate = new DataTemplate();

    //set up the label
    var labelFactory = new FrameworkElementFactory(typeof (Label)) {Name = "FooterLabel"};
    labelFactory.SetValue(VisibilityProperty, Visibility.Collapsed);
    labelFactory.SetValue(ContentProperty, new Binding());

    dataTemplate.VisualTree = labelFactory;

    var dataTrigger = new DataTrigger
    {
        Binding = new Binding("PhysicalPageNumber")
        {
            RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor)
            {
                AncestorType = typeof (ReportPagePresenter)
            }
        },
        Value = totalPages
    };

    dataTrigger.Setters.Add(new Setter {
        TargetName = "FooterLabel",
        Property = VisibilityProperty,
        Value = Visibility.Visible
    });

    dataTemplate.Triggers.Add(dataTrigger);

    return dataTemplate;
}

我希望这会有所帮助。不过我会谨慎使用它。因为现在我们两次生成报告(一次用于获取总页数并设置正确的数据模板,在正确的页面上显示页脚,再次打印它),它会导致两倍的时间,所以如果你有一个巨大的网格,这会冻结UI一段时间。