dotnet/wpf · error · InvalidOperationException
SR.NoElement
Error message
SR.NoElement
What it means
TextContainer's validation helper requires the given TextPointer's parent to be a TextElement. When the parent is null or not a TextElement (e.g. the pointer is scoped to the root TextContainer itself), the container throws InvalidOperationException(SR.NoElement) because there is no containing element to operate on. This is an internal invariant guard for pointer scoping in the WPF text stack.
Solutions
- Ensure the TextPointer's parent is a TextElement before calling element-scoped APIs (e.g. check pointer.Parent as TextElement).
- Re-position the pointer so it is inside a TextElement, or use container-scoped APIs instead.
- Refresh stale pointers against the current tree generation after edits.
- Wrap in try/catch for InvalidOperationException when working with volatile pointer positions.
Example fix
// before
var element = (TextElement)textPointer.Parent;
// after
var element = textPointer.Parent as TextElement;
if (element == null) throw new ArgumentException("Pointer is not inside a TextElement"); Defensive patterns
Strategy: type-guard
Validate before calling
bool canUse = textPointer.Parent is TextElement;
if (!canUse) throw new InvalidOperationException("Pointer must be inside a TextElement"); Type guard
TextElement elt = textPointer?.Parent as TextElement; bool isValid = elt != null;
Try / catch
try { containerApi(pointer); }
catch (InvalidOperationException ex) when (ex.Message.Contains("NoElement") || ex.Message.StartsWith("Text element")) { /* fallback: re-acquire pointer inside a TextElement */ } Prevention
- Always check TextPointer.Parent before element-scoped operations
- Re-validate pointers after tree mutations
- Avoid holding pointers across multiple edit operations
When it happens
Trigger: Calling TextContainer APIs (e.g. validation helpers used by TextPointer/TextElement operations) with a TextPointer whose Parent is the TextContainer root or otherwise not a TextElement.
Common situations: Using stale TextPointer positions after tree edits/generations changed; obtaining positions at document scope (Paragraph-less containers) and passing them to element-scoped APIs; programmatic text manipulation in RichTextBox/TextBox scenarios.
Understand the failure class
Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.
Related errors
- SR.TextPositionIsFrozen
- Animation_Invalid_DefaultValue
- ArgumentNullException(paramName)
- Cannot remove signature from read-only file.
- Image_EncoderNoColorContext
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/10238614a58e6a89.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/TextContainer.cs:3223
#region ValidateXXXHelpers
// Validation for SetValue.
private void ValidateSetValue(TextPointer position)
{
TextElement element;
if (position.TextContainer != this)
{
throw new InvalidOperationException(SR.Format(SR.NotInThisTree, "position"));
}
position.SyncToTreeGeneration();
element = position.Parent as TextElement;
if (element == null)
{
throw new InvalidOperationException(SR.NoElement);
}
}
#endregion ValidateXXXHelpers
// Verifies the TextContainer symbol count is in synch with the TextTreeText
// character count.
private void AssertTreeAndTextSize()
{
if (Invariant.Strict)
{
int count;
TextTreeTextBlock textBlock;
if (_rootNode.RootTextBlock != null)
{
count = 0;
View on GitHub (pinned to 81131a70a4)