在flowdocument中创建RTF运行

时间:2017-02-13 19:40:29

标签: c# rtf flowdocument

有谁可以帮我理解RTF?我从某个字符串中填充了inputStream

inputStream = new MemoryStream(Encoding.ASCII.GetBytes((myObject.someStr)));

然后将此流传递给flowdocument

FlowDocument fldoc = new FlowDocument();
TextRange tr = new TextRange(
    fldoc.ContentStart, fldoc.ContentEnd);
tr.Load(inputStream, DataFormats.Rtf);

之后我创建了自定义文本框来处理myObject的属性

Block curBlock = fldoc.Blocks.FirstBlock;
Inline curInline = (currentBlock as Paragraph).Inlines.FirstInline;
Run curRun = (currentInline as Span).Inlines.FirstInline as Run;
return new CustomTextBox(tr, currentRun.ContentStart, myObject)

失去焦点后,我的自定义文本框

TextPointer ptr = CurrentRun.ContentStart;
ptr.DeleteTextInRun(ptr.GetTextRunLength(LogicalDirection.Forward));
ptr.InsertTextInRun(mainTextbox.Text);
using (MemoryStream ms = new MemoryStream())
{
    tr.Save(ms, DataFormats.Rtf);
    myObject.someStr = ASCIIEncoding.Default.GetString(ms.ToArray());
}

现在问题是:如果myObject.someStr == "",那么curBlock == null,我无法实例化CustomTextBox。我想

Paragraph p = new Paragraph();
Span s = new Span();
Run r = new Run(tr.Text);
s.Inlines.Add(r);
p.Inlines.Add(s);
fldoc.Blocks.Add(new Paragraph());
return new CustomTextBox(tr, r.ContentStart, myObject)

,但无论如何tr都是空的。我该怎么处理?

1 个答案:

答案 0 :(得分:0)

fldoc = new FlowDocument(p);

帮助