简体   繁体   中英

Create RTF run in flowdocument

Could anyone please help me understand RTF? I have an inputStream filled from some string

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

Then this stream passed to flowdocument

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

After that I create custom textbox to handle myObject 's property

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)

Upon losing focus my custom text box does

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());
}

Now the problem: if myObject.someStr == "" , then curBlock == null , and I can not instantiate CustomTextBox. I am trying to

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)

, but tr is empty anyways. How can I handle that?

fldoc = new FlowDocument(p);

帮助

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM