如何将多个FlowDocument串联到1个FlowDocument中 [英] How to concatenate multiple FlowDocuments together into 1 FlowDocument

查看:267
本文介绍了如何将多个FlowDocument串联到1个FlowDocument中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多个FlowDocuments,我想将它们串联在一起. 下面的方法没有return语句. 我想做的是将TextRange重新转换为FlowDocument.

I have multiple FlowDocuments that I would like to concatenate together. The method below doesn't have a return statement. What I would like to do is to turn the TextRange back into a FlowDocument.

private FlowDocument Concatenate(FlowDocument source, FlowDocument target)
{   using(MemoryStream ms = new MemoryStream())
    {
      TextRange tr = new TextRange(source.ContentStart, source.ContentEnd);
      tr.Save(ms, DataFormats.XamlPackage);
      ms.Seek(0, SeekOrigin.Begin);
      tr = new TextRange(target.ContentEnd, target.ContentEnd);
      tr.Load(ms, DataFormats.XamlPackage);
   }
}

推荐答案

由于FlowDocuments基本上只是块集合,因此有可能并且更加简洁地从源文档中提取集合作为块列表,然后插入.那些到目标文档中.确保使用ToList()提取块,否则将出现对象已属于​​另一个集合"的错误提示

Since FlowDocuments are just basically block collections, it is possible, and much cleaner, to simply extract the collection from the source document as a list of blocks and then insert those into the target document. Make sure to extract the blocks using ToList() or else you will get an error along the lines of "object already belongs to another collection"

尝试一下(未试用):

'targetDocument is flowdocument that will be aggregate of both
'insertDocument contains document content you want to insert into target
 Dim insertBlocks As List(Of Block) = insertDocument.Blocks.ToList()
 targetDocument.Blocks.AddRange(insertBlocks)

这篇关于如何将多个FlowDocument串联到1个FlowDocument中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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