动态绑定流文档中的文本

时间:2013-12-05 12:08:16

标签: c# wpf xaml

你好我有流文档我已经使用了我的自定义模板现在我要显示我希望从表中显示文本的段落但是如何在页面中控制此模板我保存了我的报告视图  `     

    <!-- Style for header/footer rows. -->
    <Style x:Key="headerFooterRowStyle" TargetType="{x:Type TableRowGroup}">
        <Setter Property="FontWeight" Value="DemiBold"/>
        <Setter Property="FontSize" Value="16"/>
        <Setter Property="Background" Value="LightGray"/>


    </Style>

    <!-- Style for data rows. -->
    <Style x:Key="dataRowStyle" TargetType="{x:Type TableRowGroup}">
        <Setter Property="FontSize" Value="12"/>
    </Style>

    <!-- Style for data cells. -->
    <Style TargetType="{x:Type TableCell}">
        <Setter Property="Padding" Value="0.1cm"/>
        <Setter Property="BorderBrush" Value="Black"/>
        <Setter Property="BorderThickness" Value="0.01cm"/>
    </Style>
</FlowDocument.Resources>
<xrd:ReportProperties>        
    <xrd:ReportProperties.ReportTitle>Dynamic Report</xrd:ReportProperties.ReportTitle>
</xrd:ReportProperties>


<Section Padding="80,20,40,10" FontSize="12" BreakPageBefore="True">
    <Paragraph>

        <Image Name="Logo" Width="5cm" Height="5cm" />
    </Paragraph>
    <List>
        <ListItem>
            <Paragraph>ListItem 1</Paragraph>
        </ListItem>

    </List>
    <Table  CellSpacing="0"  BorderThickness="0">
        <TableRowGroup Style="{StaticResource dataRowStyle}">
            <xrd:TableRowForDynamicDataTable TableName="Data3"/>
        </TableRowGroup>
    </Table>

    <Paragraph></Paragraph>

    <Table  CellSpacing="0"  BorderThickness="0">

        <!--<TableRowGroup Foreground="Black">
            <xrd:TableRowForDynamicHeader TableName="Header2"/>
        </TableRowGroup>-->

        <TableRowGroup Style="{StaticResource dataRowStyle}">
            <xrd:TableRowForDynamicDataTable TableName="Data2"/>
        </TableRowGroup>



    </Table>

    <Table CellSpacing="0" BorderBrush="Black" BorderThickness="0.02cm">

        <TableRowGroup Style="{StaticResource headerFooterRowStyle}" Foreground="Black">
            <xrd:TableRowForDynamicHeader TableName="Header"/>

        </TableRowGroup>

        <TableRowGroup Style="{StaticResource dataRowStyle}">
            <xrd:TableRowForDynamicDataTable TableName="Data">
            </xrd:TableRowForDynamicDataTable>
        </TableRowGroup>


        <!--<TableRowGroup>
            <xrd:TableRowForDataTable 
        </TableRowGroup>-->

    </Table>
    <Table  CellSpacing="0"  BorderThickness="0">
        <TableRowGroup Style="{StaticResource headerFooterRowStyle}" Foreground="Black">
            <xrd:TableRowForDynamicHeader TableName="Header4"/>
        </TableRowGroup>

        <TableRowGroup Style="{StaticResource dataRowStyle}">
            <xrd:TableRowForDynamicDataTable TableName="Data4">

            </xrd:TableRowForDynamicDataTable>
        </TableRowGroup>
    </Table>

</Section>

`这是我的xaml文件,我现在推出了我的报告查看器,我想在此报告中显示我希望从文件中显示的文本

namespace POS_System
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private bool _firstActivated = true;
        public string _entitiid;
        public string _entrytrype;
        ModelInvoice _ModelInvoice = new ModelInvoice();
        ModelOrderCustomer _Modelordercustomer = new ModelOrderCustomer();
        ModelLogin _Modellogin = new ModelLogin();

        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Activated(object sender, EventArgs e)
        {
            if (!_firstActivated) return;

            _firstActivated = false;

            try
            {


                ReportDocument reportDocument = new ReportDocument();

                string folderpath = @"E:\POS_System\POS_System\Templates\DynamicReport.xaml";
                StreamReader reader = new StreamReader(new FileStream(folderpath, FileMode.Open, FileAccess.Read));
                reportDocument.XamlData = reader.ReadToEnd();
                string path = @"E:\Projects\POS_System\POS_System\";
                reportDocument.XamlImagePath = System.IO.Path.Combine(path, @"Templates\");
                reader.Close();


                ReportData data = new ReportData();

                data.ReportDocumentValues.Add("PrintDate", DateTime.Now); // print date is now
                DateTime dateTimeStart = DateTime.Now; // start time measure here

                DataTable dt = new DataTable();
                dt = _ModelInvoice.FillProductDetail(_entitiid);

                DataTable tableHeader = null;
                DataTable tableData = null;


                tableHeader = new DataTable("Header");
                tableData = new DataTable("Data");
                tableHeader.Columns.Add();

                tableHeader.Rows.Add(new object[] { "Product" });
                tableHeader.Rows.Add(new object[] { "Price" });
                tableHeader.Rows.Add(new object[] { "Quantity" });
                tableHeader.Rows.Add(new object[] { "Discount%" });
                tableHeader.Rows.Add(new object[] { "Discounted Price" });
                tableHeader.Rows.Add(new object[] { "Vat rate" });
                tableHeader.Rows.Add(new object[] { "Sum" });



                tableData = dt.Copy();

                tableData.Columns.Add("temp", typeof(int), "Convert(Totalprices, 'System.Int32')");

               object sumObject;
               sumObject = tableData.Compute("Sum(temp)", "");


                data.DataTables.Add(tableData);
                data.DataTables.Add(tableHeader);
                data.DataTables[0].TableName = "Data";
                data.DataTables[1].TableName = "Header";

                tableHeader = new DataTable("Header2");
                tableHeader.Columns.Add();
                tableHeader.Rows.Add(new object[] { "Customerdata" });
                tableHeader.Rows.Add(new object[] { "Place of issue" });
                tableHeader.Rows.Add(new object[] { "Document date" });
                tableHeader.Rows.Add(new object[] { "Delivery date" });
                tableHeader.Rows.Add(new object[] { "Due date" });
                tableHeader.Rows.Add(new object[] { "Payment days" });




                tableData = new DataTable("Data2");

                DataTable dt1 = _Modelordercustomer.GetRecodeuserDetails(_entitiid, _entrytrype);

                tableData.Columns.Add("Customerdata");


                DateTime _documentdate, _deliverydate;
                string _strdocumentdate, _strdeliverydate, _strduedate;
                int PaymentDays = 0;
                foreach (DataRow d in dt1.Rows)
                {
                    DataRow row;
                    row = tableData.NewRow();
                    string Address, Placeofissue, Customerdata;
                    Address = d["customer"].ToString() + d["street_address"].ToString() + d["zip"].ToString() + d["city"].ToString() + d["state"].ToString();
                    Placeofissue = d["location_name"].ToString();

                    _documentdate = Convert.ToDateTime(d["document_date"].ToString());
                    _strdocumentdate = _documentdate.ToString("dd MMM yyyy");
                    _deliverydate = Convert.ToDateTime(d["delivery_date"].ToString());
                    _strdeliverydate = _deliverydate.ToString("dd MMM yyyy");
                    PaymentDays = Convert.ToInt32(d["entity_payment"]);
                    _strduedate = _deliverydate.AddDays(PaymentDays).ToString("dd MMM yyyy");
                    Customerdata = Address + Placeofissue + _strdeliverydate + _strdocumentdate + _strduedate;
                    row["Customerdata"] = Customerdata;
                    tableData.Rows.Add(row);

                }

                data.DataTables.Add(tableData);




                data.DataTables[2].TableName = "Data2";
              //  data.DataTables[3].TableName = "Header2";


                tableData = new DataTable("Data3");
                DataTable dt2 = _Modellogin.GetShopAddress();
                tableData.Columns.Add("Shopaddress");

                tableData = dt2.Copy();
                data.DataTables.Add(tableData);
                data.DataTables[3].TableName = "Data3";

                List<DataRow> list = new List<DataRow>();
                foreach (DataRow dr in dt2.Rows)
                {
                    list.Add(dr);
                }






                tableHeader = new DataTable("Header4");
                tableHeader.Columns.Add();
                tableHeader.Rows.Add(new object[] { "VAT rate" });
                tableHeader.Rows.Add(new object[] { "VAT base" });
                tableHeader.Rows.Add(new object[] { "VAT amount" });
                tableData = new DataTable("Data4");
                tableData.Columns.Add("VAT rate");
                tableData.Columns.Add("VAT base");
                tableData.Columns.Add("VAT amount");


                foreach (DataRow d in dt.Rows)
                {
                    DataRow row;
                    row=tableData.NewRow();
                    row["VAT rate"] = d["tax_type"].ToString();
                    row["VAT base"] = d["Totalprices"].ToString();
                    row["VAT amount"] = d["vatamount"].ToString();
                    tableData.Rows.Add(row);


                }


                data.DataTables.Add(tableData);
                data.DataTables.Add(tableHeader);
                data.DataTables[4].TableName = "Data4";
                data.DataTables[5].TableName = "Header4";

                reportDocument.ImageProcessing += reportDocument_ImageProcessing;
                reportDocument.ImageError += reportDocument_ImageError;
                System.Windows.Xps.Packaging.XpsDocument xps = reportDocument.CreateXpsDocument(data);

                documentViewer.Document = xps.GetFixedDocumentSequence();

            }
            catch (Exception ex)
            {
                // show exception
                MessageBox.Show(ex.Message + "\r\n\r\n" + ex.GetType() + "\r\n" + ex.StackTrace, ex.GetType().ToString(), MessageBoxButton.OK, MessageBoxImage.Stop);
            }



        }
        public static readonly DependencyProperty DocumentProperty =
      DependencyProperty.Register("Document", typeof(FlowDocument), typeof(Test),
     new PropertyMetadata(OnDocumentChanged));
        public FlowDocument Document
        {
            get { return (FlowDocument)GetValue(DocumentProperty); }
            set { SetValue(DocumentProperty, value); }
        }
        private static void OnDocumentChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            //var control = (MainWindow)d;
            //control..Text = "Pallavi";


            //else
            //control.RTB. =e.NewValue;
            ////else
            //    control.RTB.Document = new FlowDocument(new RichTextBox(new Run(control.RTB.te)));
            //else
            //    control.RTB.Document = e.NewValue;
            //?


            //RichTextBoxControl control = (RichTextBoxControl)d;
            //if (e.NewValue == null)
            //    control.txtname.Document = new FlowDocument(); //Document is not amused by null :)
            //else
            //    control.txtname.Document = e.NewValue;


        }

        private void reportDocument_ImageError(object sender, ImageErrorEventArgs e)
        {
            e.Handled = true; // just suppress exceptions
        }
        private void reportDocument_ImageProcessing(object sender, ImageEventArgs e)
        {


            if (e.Image.Name == "Logo")
            {
                ModelLogin _ModelLogin = new ModelLogin();

                string imagepath = _ModelLogin.Getlogoimage();
                var image = new BitmapImage();
                image.BeginInit();
                image.UriSource = new Uri(imagepath);

                image.EndInit();
                e.Image.Source = image;

            }




        }


    }
}

0 个答案:

没有答案