在将一个RichTextBox的FlowDocument分配给另一个时,我得到一个异常,即FlowDocument属于另一个TextBox。如何分配。
public ZoomedDialog(FlowDocument pFlowDocument)
{
txtRichContent.Document = pFlowDocument;
// this line throws exception "pFlowDocument belongs to another RichTextBox"
}
答案 0 :(得分:2)
在MSDN论坛上查看此问题:http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/2bc414fb-bcb9-4ecc-bb27-b55870085f1f/
在SO上查看这个问题:Sharing FlowDocuments between multiple RichTextBoxes
根据您显示的代码,我无法确定适合您的设置。
答案 1 :(得分:1)
人,
试试这个解决方案:)!
public static void OnDocumentChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
{
var rtb = (RichTextBox)obj;
if (args.NewValue != null)
{
var doc = (FlowDocument) args.NewValue;
if (doc.Tag is RichTextBox)
{
// clear belongs to another rtb.
((RichTextBox) doc.Tag).Document = new FlowDocument();
}
else
{
doc.Tag = rtb;
}
rtb.Document =doc;
}
}