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); } } }
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); if (length != text.Length || text != context.Document.GetText(offset, length)) { // Call replace only if we're actually changing something. // Without this check, we would generate an empty undo group when the user pressed undo. 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); } } } }