// Remove InlineObjectRun.Element from TextLayer. // Caller of RemoveInlineObjectRun will remove it from inlineObjects collection. void RemoveInlineObjectRun(InlineObjectRun ior) { if (ior.Element.IsKeyboardFocusWithin) { // When the inline element that has the focus is removed, WPF will reset the // focus to the main window without raising appropriate LostKeyboardFocus events. // To work around this, we manually set focus to the next focusable parent. UIElement element = textView; while (element != null && !element.Focusable) { element = VisualTreeHelper.GetParent(element) as UIElement; } if (element != null) { Keyboard.Focus(element); } } ior.VisualLine = null; RemoveVisualChild(ior.Element); }
public override TextRun GetTextRun(int textSourceCharacterIndex) { try { foreach (VisualLineElement element in VisualLine.Elements) { if (textSourceCharacterIndex >= element.VisualColumn && textSourceCharacterIndex < element.VisualColumn + element.VisualLength) { int relativeOffset = textSourceCharacterIndex - element.VisualColumn; TextRun run = element.CreateTextRun(textSourceCharacterIndex, this); if (run == null) { throw new ArgumentNullException(element.GetType().Name + ".CreateTextRun"); } if (run.Length == 0) { throw new ArgumentException("The returned TextRun must not have length 0.", element.GetType().Name + ".Length"); } if (relativeOffset + run.Length > element.VisualLength) { throw new ArgumentException("The returned TextRun is too long.", element.GetType().Name + ".CreateTextRun"); } InlineObjectRun inlineRun = run as InlineObjectRun; if (inlineRun != null) { inlineRun.VisualLine = VisualLine; TextView.textLayer.AddInlineObject(inlineRun); } return(run); } } return(new TextEndOfParagraph(1)); } catch (Exception ex) { Debug.WriteLine(ex.ToString()); throw; } }
/// <summary> /// Adds a new inline object. /// </summary> internal void AddInlineObject(InlineObjectRun inlineObject) { inlineObjects.Add(inlineObject); AddVisualChild(inlineObject.Element); inlineObject.Element.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity)); }