在WPF中将PageContent / FixedPage添加到FixedDocument的正确方法是什么?

时间:2013-03-07 09:36:46

标签: wpf document fixeddocument

在WPF中,为了在代码中向FixedPage添加FixedDocument,我们需要:

var page = new FixedPage();
var pageContent = new PageContent();

((IAddChild)pageContent).AddChild(page);

然而,这似乎是唯一的方法:

  • MSDN文档明确表示不应该这样做('此API支持.NET Framework基础结构,不能直接在您的代码中使用。' - PageContent.IAddChild.AddChild Method)。

  • 为了将内容添加到PageContent,必须强制转换为显式接口实现。

  • 执行PageContent的基本操作并不简单。

文档实际上并未解释如何执行此操作,但我找不到有关如何执行此操作的任何其他信息。还有另外一种方法吗?一个'正确'的方式?

1 个答案:

答案 0 :(得分:7)

根据MSDN文档,您只需将FixedPage对象添加到PageContent.Child属性,然后通过调用FixedDocument方法将其添加到FixedDocument.Pages.Add

例如:

public FixedDocument RenderDocument() 
{
    FixedDocument fd = new FixedDocument();
    PageContent pc = new PageContent();
    FixedPage fp = new FixedPage();
    TextBlock tb = new TextBlock();

    //add some text to a TextBox object
    tb.Text = "This is some test text";
    //add the text box to the FixedPage
    fp.Children.Add(tb);
    //add the FixedPage to the PageContent 
    pc.Child = fp;
    //add the PageContent to the FixedDocument
    fd.Pages.Add(pc);

    return fd;
}
相关问题