【问题标题】:Forcing a FlowDocument to Load?强制加载 FlowDocument?
【发布时间】:2012-06-14 17:21:10
【问题描述】:

我正在尝试打印出用户正在查看的 FlowDocument。我编写了一个例程来创建原始文档的副本,这样更改(来自 PrintDialog)就不会反映到 DocumentViewer 中。

不幸的是,我的副本似乎丢失了绑定到其字段的所有信息。我已经尝试重置 DataContext,但副本的 IsLoaded 属性仍然返回 false,让我相信绑定没有发生。

有什么想法吗?

这是我用来复制文档的代码:

private static void AddDocument(FlowDocument from, FlowDocument to)
{
    TextRange tr = new TextRange(from.ContentStart, from.ContentEnd);

    using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
    {
        TextRange tr2 = null;

        System.Windows.Markup.XamlWriter.Save(tr, ms);
        tr.Save(ms, DataFormats.XamlPackage, true);
        tr2 = new TextRange(to.ContentEnd, to.ContentEnd);
        tr2.Load(ms, DataFormats.XamlPackage);
    }
}

这是我用来打印文档的代码:

public static void PrintFlowDocument(FlowDocument fd, string title)
{
    PrintDialog pd = new PrintDialog();
    IDocumentPaginatorSource idps = null;
    FlowDocument flowDoc = new FlowDocument();

    AddDocument(fd, flowDoc);

    flowDoc.DataContext = fd.DataContext;
    flowDoc.PageHeight = pd.PrintableAreaHeight;
    flowDoc.PageWidth = pd.PrintableAreaWidth;
    flowDoc.PagePadding = new Thickness(50);
    flowDoc.ColumnGap = 0;
    flowDoc.ColumnWidth = pd.PrintableAreaWidth;


    idps = flowDoc;

    if (pd.ShowDialog() == true)
    {
        pd.PrintDocument(idps.DocumentPaginator, title);
    }
}

提前致谢,
桑尼

【问题讨论】:

    标签: c# wpf printing flowdocument


    【解决方案1】:

    注意到这条线的一些地方了吗?

    tr2 = new TextRange(to.ContentEnd, to.ContentEnd);
    

    【讨论】:

    • 不...它只是将 TextRange 定位到文档的末尾。
    【解决方案2】:

    我遇到了类似的问题,并发现强制将文档创建到后台线程使绑定有机会触发。否则,它不会发生在 UI 线程上。

    所以,如果你的复制文档方法是一个函数,它会是这样的:

                Dim flowDoc As FlowDocument = 
                    DirectCast(<ViewInstance>.UIDispatcher.Invoke(Function() 
                                   AddFlowDocument(fd),
                                   Windows.Threading.DispatcherPriority.Background), 
                               FlowDocument)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-16
      • 2019-04-16
      • 1970-01-01
      相关资源
      最近更新 更多