I am working with FlowDocuments in a RichTextBox, more specifically with TextRanges and TextPointers when TextChange Events occur.
I am accessing the TextRange through
TextChange change;
TextRange range = new TextRange(RichTextControl.Document.ContentStart, RichtextControl.Document.ContentEnd);
TextRange changeRange = new TextRange(range.Start.GetPositionAtOffset(change.Offset), range.Start.GetPositionAtOffset(change.Offset+change.AddedLength));
The TextRange I have to work with spans over multiple Paragraphs. The TextRange I extract from the selection, simplified, looks like this:
<Section>
<Paragraph>
<Span>
<Run>Test1</Run>
</Span>
</Paragraph>
<Paragraph>
<Span>
<Run>Test2</Run>
</Span>
</Paragraph>
</Section>
I can now access the first Paragraph my using TextRange.Start. I can traverse this paragraph normally through accessing Inlines.
However, as soon as I want to access the SECOND paragraph I’m somewhat out of options. I can see TextPointer.GetNextContextPosition, but that would lead to me hopping over the Textpointer Offset positions until the Context the pointer is in changes, which, I hope, just can’t be the way to do this.
I don’t know if I am just doing it wrong to actually capture the changed text, or if I am just missing the right Interface to traverse all Paragraphs contained in the Section. If so, the solution should be easy, but I just can’t see it.
I seemed to have lacked the proper search terms to find the solution. I found something that might be usable here http://www.bryanewert.net/journal/2010/5/26/how-to-explore-the-contents-of-a-flowdocument.html