我有一個RichTextBox和DocumentViewer(放置在TabControl中)的應用程序,我想做一些類似「熱預覽」的東西。我已經綁定DocumentViewer.Document
屬性RichTextBox.Document
DocumentViewer到RichTextBox綁定錯誤
綁定:
<DocumentViewer Document="{Binding Document, Converter={StaticResource FlowDocumentToPaginatorConverter}, ElementName=mainRTB, Mode=OneWay}" />
這是轉換器代碼:
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
FlowDocument d = value as FlowDocument;
DocumentPaginator pagin = ((IDocumentPaginatorSource)d).DocumentPaginator;
FixedDocumentSequence result = null;
Size s = new Size(793.700787402, 1122.519685039);
pagin.PageSize = s;
using (MemoryStream ms = new MemoryStream())
{
TextRange tr = new TextRange(d.ContentStart, d.ContentEnd);
tr.Save(ms, DataFormats.XamlPackage);
Package p = Package.Open(ms, FileMode.Create, FileAccess.ReadWrite);
Uri uri = new Uri(@"memorystream://doc.xps");
PackageStore.AddPackage(uri, p);
XpsDocument xpsDoc = new XpsDocument(p);
xpsDoc.Uri = uri;
XpsDocument.CreateXpsDocumentWriter(xpsDoc).Write(pagin);
result = xpsDoc.GetFixedDocumentSequence();
}
return result;
}
當我開始這個應用程序一切正常,直到我切換到與標籤的DocumentViewer。應用程序粉碎,我得到這樣的例外:
無法在只寫模式下執行讀取操作。
我在做什麼錯了?是否有可能使這種綁定?
按承諾的代碼示例更新。 – Dennis 2012-03-12 09:36:44
如果你正在尋找'ForceRenderFlowDocument'背後的魔法,它可以在這個StackOverflow答案中找到。 http://stackoverflow.com/questions/9447338/printing-blockuicontainer-to-xpsdocument-fixeddocument – Dennis 2012-03-14 13:22:31