I am completely new to FlowDocuments and can't seem to find this anywhere.
I created a TextBlock where each Inline is a Run containing exactly one word. (The Inlines are created in code). I then use TextRange to replace some letters within the Run. But instead of replacing the characters of the Run, the TextRange seems to empty the Run and append the letters to the previous Run.
(It is important to retain the original TextBlock structure with the original Run objects as I have events tied to them.)
What am I doing wrong? And what is the TextRange.Text = text actually doing?
Any help is appreciated.
Example
---Before textrange--
<Run>
6||perrla
</Run>
<Run>
1|| **NOTE THIS IS A SPACE IN THE RUN
</Run>
<Run>
5||nc/at
</Run>
--After textrange ---
<Run> **NOTE THE EMPTY RUN
</Run>
<Run>
5|| test **NOTE HOW TEST IS APPENDED TO THE SPACE OF THE PREVIOUS RUN
</Run>
<Run>
5||nc/at
</Run>
The code that produce the above results:
string text = nw.Replace("\0", string.Empty);
TextPointer s = StartInlineElement.GetInsertionPosition(LogicalDirection.Forward);
TextPointer start = s.GetPositionAtOffset(StartIndex);
TextPointer end = start.GetPositionAtOffset(Text.Length);
var y = start.GetAdjacentElement(LogicalDirection.Backward);
if (y != null)
{
do
{
GetXaml((TextElement)y);
y = (y as Run).PreviousInline;
} while (y != null);
}
THIS IS WRONG -- IT EMPTIES THE RUN AND APPENDS TO THE RUN
BEFORE IT.
TextRange tr = new TextRange(start, end);
tr.Text = text;
y = start.GetAdjacentElement(LogicalDirection.Forward);
if (y != null)
{
do
{
GetXaml((TextElement)y);
y = (y as Run).PreviousInline;
} while (y != null);
}
}
GetXaml is from Microsoft (https://msdn.microsoft.com/en-us/library/system.windows.documents.textpointercontext(v=vs.110).aspx) which I slighltly modified to:
void GetXaml(TextElement element)
{
// Position a "navigator" pointer before the opening tag of the element.
TextPointer navigator = element.ElementStart;
while (navigator.CompareTo(element.ElementEnd) < 0)
{
switch (navigator.GetPointerContext(LogicalDirection.Forward))
{
case TextPointerContext.ElementStart:
// Output opening tag of a TextElement
Console.WriteLine(String.Format("<{0}>", navigator.GetAdjacentElement(LogicalDirection.Forward).GetType().Name));
break;
case TextPointerContext.ElementEnd:
// Output closing tag of a TextElement
Console.WriteLine(String.Format("</{0}>", navigator.GetAdjacentElement(LogicalDirection.Forward).GetType().Name));
break;
case TextPointerContext.EmbeddedElement:
// Output simple tag for embedded element
Console.WriteLine(String.Format("<{0}>", navigator.GetAdjacentElement(LogicalDirection.Forward).GetType().Name));
break;
case TextPointerContext.Text:
// Output the text content of this text run
Console.WriteLine(String.Format("{0}||{1}", navigator.GetTextRunLength(LogicalDirection.Forward),
navigator.GetTextInRun(LogicalDirection.Forward)));
break;
}
// Advance the naviagtor to the next context position.
navigator = navigator.GetNextContextPosition(LogicalDirection.Forward);
}
}