我正在尝试打印出用户正在查看的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);
}
}提前谢谢你,
桑尼
发布于 2012-06-15 01:48:13
注意到这一行的一些错误了吗?
tr2 = new TextRange(to.ContentEnd, to.ContentEnd);发布于 2012-06-15 20:38:57
我也有类似的问题,我发现强制文档创建到后台线程给了绑定一个触发的机会。否则,它不会在UI线程上发生。
因此,如果您的复制文档方法是一个函数,它将如下所示:
Dim flowDoc As FlowDocument =
DirectCast(<ViewInstance>.UIDispatcher.Invoke(Function()
AddFlowDocument(fd),
Windows.Threading.DispatcherPriority.Background),
FlowDocument)https://stackoverflow.com/questions/11038213
复制相似问题