void targetElement_TextChanged(object sender, EventArgs e)
{
// Don't copy text if the segments overlap (we would get an endless loop).
// This can happen if the user deletes the text between the replaceable element and the bound element.
if (segment.GetOverlap(targetElement.Segment) == SimpleSegment.Invalid)
{
int offset = segment.Offset;
int length = segment.Length;
string text = boundElement.ConvertText(targetElement.Text);
context.Document.Replace(offset, length, text);
if (length == 0)
{
// replacing an empty anchor segment with text won't enlarge it, so we have to recreate it
segment = new AnchorSegment(context.Document, offset, text.Length);
}
}
}