{"record":{"id":"dd37a0cc7ffd60e8","repo":"dotnet/wpf","slug":"sr-textschema-uielementnotallowedinthisposition","errorCode":null,"errorMessage":"SR.TextSchema_UIElementNotAllowedInThisPosition","messagePattern":"SR\\.TextSchema_UIElementNotAllowedInThisPosition","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/TextPointer.cs","lineNumber":2147,"sourceCode":"        /// The LogicalDirection property specifies whether this TextPointer\n        /// will be positioned before or after the UIElement.\n        /// </remarks>\n        /// <exception cref=\"ArgumentException\">\n        /// Throws ArgumentException is contentElement is not valid\n        /// according to flow schema.\n        /// </exception>\n        internal void InsertUIElement(UIElement uiElement)\n        {\n            ArgumentNullException.ThrowIfNull(uiElement);\n\n            _tree.EmptyDeadPositionList();\n            SyncToTreeGeneration();\n\n            ValidationHelper.ValidateChild(this, uiElement, nameof(uiElement));\n\n            if (!((TextElement)this.Parent).IsEmpty) // the parent may be InlineUIContainer or BlockUIContainer\n            {\n                throw new InvalidOperationException(SR.TextSchema_UIElementNotAllowedInThisPosition);\n            }\n\n            _tree.BeginChange();\n            try\n            {\n                _tree.InsertEmbeddedObjectInternal(this, uiElement);\n            }\n            finally\n            {\n                _tree.EndChange();\n            }\n        }\n\n        // consider adding this to public API.\n        internal TextElement GetAdjacentElementFromOuterPosition(LogicalDirection direction)\n        {\n            TextTreeTextElementNode elementNode;\n","sourceCodeStart":2129,"sourceCodeEnd":2165,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/TextPointer.cs#L2129-L2165","documentation":"TextPointer.InsertUIElement inserts a UIElement as an embedded object at the pointer's position. WPF requires the enclosing element to be empty (an InlineUIContainer or BlockUIContainer that already has content cannot host another child), so when this.Parent is a non-empty TextElement it throws InvalidOperationException(SR.TextSchema_UIElementNotAllowedInThisPosition).","triggerScenarios":"Calling InsertUIElement on a TextPointer whose parent TextElement is a non-empty InlineUIContainer/BlockUIContainer, or whose position does not permit embedded objects per the text schema.","commonSituations":"Programmatically inserting images/controls into a RichTextBox FlowDocument at a position that already contains a UIElement; inserting at a position where validation placed the pointer inside a populated container.","solutions":["Check ((TextElement)pointer.Parent).IsEmpty before calling InsertUIElement, or create a fresh InlineUIContainer at a new position.","Move the TextPointer to an empty insertion point (e.g. after the existing element) before inserting.","Wrap in try/catch for InvalidOperationException and fall back to inserting a new Run/container."],"exampleFix":"// before\npointer.InsertUIElement(myImage);\n// after\nif (pointer.Parent is TextElement te && te.IsEmpty)\n    pointer.InsertUIElement(myImage);\nelse {\n    var container = new InlineUIContainer(myImage);\n    ((Paragraph)pointer.Parent).Inlines.InsertBefore(pointer.GetAdjacentElement(LogicalDirection.Backward) as Inline, container);\n}","handlingStrategy":"validation","validationCode":"if (!(pointer.Parent is TextElement parent) || !parent.IsEmpty)\n    throw new InvalidOperationException(\"Position cannot host a UIElement.\");","typeGuard":"static bool CanInsertUIElement(TextPointer p) => p.Parent is TextElement te && te.IsEmpty;","tryCatchPattern":"try { pointer.InsertUIElement(element); }\ncatch (InvalidOperationException) { /* reposition into a fresh InlineUIContainer and retry */ }","preventionTips":["Check Parent.IsEmpty before inserting embedded objects","Insert at freshly created containers, not existing populated ones","Verify the schema allows embedded objects at the target position"],"tags":["wpf","richtextbox","uielement","schema"],"backgroundTag":"schema-validation-failed","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}