wpf fixeddocument dynamic data

2019-05-31 01:06发布

I am creating FixedDocument and adding pages to it dynamically.

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);

    }

}

The code is still giving error that page1content is a child of another parent.

2条回答
2楼-- · 2019-05-31 01:20

It is an easy error to diagnose; an visual can only ever have one parent as otherwise you would have a circular dependency in the VisualTree. Review your code to check if you are using the same visual twice.

If you want the visual to appear in two places simultaneously then you need to duplicate it; if the second use is intentional then you can un-parent visual by removing itself from the parent. e.g. (canvas.Parent.Remove(canvas)).

In the code sample posted I can identify at least one instance of where this would occur and have detailed this below.


You are adding the instance of vCanvas to more than once in your while(loop) block. You need to create a new Canvas for each iteration.

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);
}

For the purposes of this example, I will the Canvas in the PageInit().

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

    return canvas;
}
查看更多
霸刀☆藐视天下
3楼-- · 2019-05-31 01:25

I did it. The solution is as follows

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;
    }

You can skip the canvas. It is requirment of my project so I was trying it.

查看更多
登录 后发表回答