wpf fixeddocument动态数据

时间:2012-04-12 03:05:23

标签: c# wpf fixeddocument

我正在创建FixedDocument并动态添加页面。

public partial class Window1 : Window
{
    FixedDocument fd = new FixedDocument();
    TextBlock page1Text = new TextBlock();

    public Window1()
    {
        InitializeComponent();
    }

    private void Print_Click(object sender, RoutedEventArgs e)
    {
        PrintDialog pd = new PrintDialog();


        fd.DocumentPaginator.PageSize = new Size(pd.PrintableAreaWidth, pd.PrintableAreaHeight);

        for (int i = 0; i <= 5; i++)
        {
            FixedPage page1 = new FixedPage();
            page1.Width = fd.DocumentPaginator.PageSize.Width;
            page1.Height = fd.DocumentPaginator.PageSize.Height;

            pages();
            page1.Children.Add(page1Text);
            PageContent page1Content = new PageContent();
            ((IAddChild)page1Content).AddChild(page1);
            fd.Pages.Add(page1Content);
        }

        DocumentViewer dr = new DocumentViewer();
        dr.Height = 700;
        dr.Document =fd;

        stack.Children.Add(dr);


    }

    private void pages()
    {
        page1Text.Text = "This is a test";
        page1Text.FontSize = 40;
        page1Text.Margin = new Thickness(96);

    }

}

代码仍然是错误,即page1content是另一个父级的子级。

2 个答案:

答案 0 :(得分:3)

诊断很容易出错;视觉只能永远拥有一个父级,否则您将在VisualTree中拥有循环依赖。检查您的代码以检查您是否使用相同的视觉效果两次。

如果您希望视觉同时出现在两个地方,那么您需要复制它;如果第二次使用是有意的,那么你可以通过从父级中删除自己来取消父级视觉。例如(canvas.Parent.Remove(canvas))。

在发布的代码示例中,我可以在至少一个实例中识别出这种情况,并在下面详细说明。


您要在vCanvas块中多次添加while(loop)的实例。您需要为每次迭代创建一个新的Canvas

var visual = /* unknown */;
var fd = new FixedDocument();   
while(loop)
{
    var canvas = PageInit();
    var page = new FixedPage();
    page.Width = visual.DocumentPaginator.PageSize.Width;
    page.Height = visual.DocumentPaginator.PageSize.Height;
    page.Children.Add(canvas);

    PageContent pageContent = new PageContent();
    ((IAddChild)pageContent).AddChild(page);
    visual.Pages.Add(pageContent);
}

出于本示例的目的,我将Canvas中的PageInit()

private Canvas PageInit()
{
    var tb = new TextBlock();
    tb.Text = "From Data";
    var canvas = new Canvas();
    canvas.Children.Add(tb);

    return canvas;
}

答案 1 :(得分:0)

我做到了。解决方案如下

private void Print_Click(object sender, RoutedEventArgs e)
    {
        PrintDialog pd = new PrintDialog();


fd.DocumentPaginator.PageSize = new Size(pd.PrintableAreaWidth,fd.PrintableAreaHeight);

        for (int i = 0; i <= 5; i++)
        {
            FixedPage page1 = new FixedPage();
            page1.Width = fd.DocumentPaginator.PageSize.Width;
            page1.Height = fd.DocumentPaginator.PageSize.Height;

            UIElement page1Text = pages();
            page1.Children.Add(page1Text);
            PageContent page1Content = new PageContent();
            ((IAddChild)page1Content).AddChild(page1);
            fd.Pages.Add(page1Content);
        }

        DocumentViewer dr = new DocumentViewer();
        dr.Height = 700;
        dr.Document =fd;

        stack.Children.Add(dr);


    }

    private UIElement pages()
    {
        Canvas pcan = new Canvas();

        TextBlock page1Text = new TextBlock();
        page1Text.Text = "This is a test";
        page1Text.FontSize = 40;
        page1Text.Margin = new Thickness(96);

        pcan.Children.Add(page1Text);


        return pcan;
    }

您可以跳过画布。这是我的项目的要求所以我正在尝试它。