如何将WPF FlowDocument动态绑定到DataTable [英] How to Dynamically Bind WPF FlowDocument to DataTable

查看:918
本文介绍了如何将WPF FlowDocument动态绑定到DataTable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

朋友们,我正在使用WPF Flow文档来查看数据.
我想通过DataTable与数据库动态绑定Flowdocument以以格式化的方式显示数据.
但是很遗憾,它没有向我显示数据.
以下是我的Flowdocument

Hi friends, I am using a WPF Flowdocument for viewing data.
I want to bind Flowdocument Dynamically with the database through DataTable to show the data in a formatted manner.
But unfortunately it is not showing me the data.
Following is my Flowdocument

<FlowDocument xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

           

              ColumnWidth="400" FontSize="14" FontFamily="Georgia">
    <Table DataContext="{Binding}">
        
        <TableRowGroup Paragraph.TextAlignment="left">
            <TableRow FontWeight="Bold" >
                <TableCell>
                    <Paragraph>
                        <TextBlock Text="{Binding Path=Name}" />
                       
                    </Paragraph>
                </TableCell>
                <TableCell>
                    <Paragraph>
                        <TextBlock Text="{Binding Path=Age}" />
                       
                    </Paragraph>
                </TableCell>
            </TableRow>
           
        </TableRowGroup>
    </Table>

   
</FlowDocument>



我将这个Flowdocument与DataTable绑定在一起



I am binding this Flowdocument wiith an DataTable

private DataTable DataTableC()
        {
            DataTable dt = new DataTable("TestData");
            dt.Columns.Add(new DataColumn("Name", typeof(string)));
            dt.Columns.Add(new DataColumn("Age", typeof(int)));

            DataRow dr = dt.NewRow();
            dr[0] = "Demo1";
            dr[1] = 16;
            dt.Rows.Add(dr);
            dr = dt.NewRow();
            dr[0] = "Demo2";
            dr[1] = 18;
            dt.Rows.Add(dr);
            dr = dt.NewRow();
            dr[0] = "Demo3";
            dr[1] = 22;
            dt.Rows.Add(dr);

            return dt;
        }



并将Data上下文赋予Flow文档,如下所示



And Giving the Data context to the Flow document as follows

FlowDocument document = null;
           document = Application.LoadComponent(new Uri("FlowDocument1.xaml",UriKind.Relative)) as FlowDocument;
           this.WriteXPS(document);
           document.DataContext = DataTableC();



不幸的是它没有被束缚.
任何帮助将不胜感激.
在此先感谢



Unfortuantely it is not getting binded.
Any help will be highly appreciated.
Thanks in Advance

推荐答案

开源WPF报告引擎 [ ^ ]
使用COdeRespon.Reports.dll,我们可以制作一个动态FlowDocumentReport
Open-Source WPF Reporting Engine[^]
Using COdeRespon.Reports.dll we can make a dynamic FlowDocumentReport


document.DataContext = DataTableC().DefaultView;





原因是FlowDocument 控件不支持DataBinding ,如此处 http://msdn所述. microsoft.com/en-us/magazine/dd569761.aspx [ ^ ]
虽然流程文档中有很多很棒的功能,但是如果文档是从动态数据生成的,那么您会遇到一些问题:流程文档中不支持数据绑定."
在上述参考文献中给出了解决方法.我认为这可能会有所帮助.





The reason is that the FlowDocument control does not support DataBinding as explained here http://msdn.microsoft.com/en-us/magazine/dd569761.aspx[^]
"While there are many great features in flow documents, if your documents are generated from dynamic data, you have a bit of a problem: there is no support for data binding in flow documents."
A work around is given at the above reference. I think it may be helpful.


请参考类似的讨论 [ ^ ],看看是否能从中获得帮助..
Refer Similar discussion[^], see if you get some help from it..


这篇关于如何将WPF FlowDocument动态绑定到DataTable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆