dotnet/wpf · error · ArgumentException
SR.NotInAssociatedTree
Error message
SR.NotInAssociatedTree
What it means
ValidationHelper.VerifyPosition throws ArgumentException(SR.NotInAssociatedTree) when the supplied position belongs to a different ITextContainer than the one passed in. TextPointers are only valid within their owning document; mixing pointers between documents is rejected.
Solutions
- Always obtain pointers from the same container you operate on (e.g. use document.ContentStart of THAT document).
- Re-create pointers after swapping the control's Document; old pointers are orphaned.
- Guard with pos.TextContainer == container before calling text APIs.
Example fix
// before otherDoc.TextContainer.VerifyPosition(container, pointerFromOtherDoc, nameof(pointerFromOtherDoc)); // after if (pointerFromOtherDoc.TextContainer == container) container.VerifyPosition(container, pointerFromOtherDoc, nameof(pointerFromOtherDoc));
Defensive patterns
Strategy: type-guard
Validate before calling
bool sameTree = position != null && position.TextContainer == container;
Type guard
bool BelongsToContainer(ITextPointer p, ITextContainer c) => p != null && p.TextContainer == c;
Try / catch
try { container.VerifyPosition(container, pos, nameof(pos)); }
catch (ArgumentException) { pos = container.CurrentPosition; } Prevention
- Only use pointers from the same document
- Recreate pointers after Document swaps
- Never share TextPointers across controls
When it happens
Trigger: Passing a TextPointer from one FlowDocument/TextBox to another document's API (e.g. container.VerifyPosition(otherContainer, pointerFromElsewhere, nameof(pos))).
Common situations: Copying TextPointer fields between RichTextBox instances; using a saved pointer after assigning a new FlowDocument to the control; merging selections from two documents.
Understand the failure class
Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.
Related errors
- SR.AnnotationAdorner_NotUIElement
- SR.AudioVideo_InvalidDependencyObject
- SR.Format(SR.BadFixedTextPosition, "position")
- SR.Format(SR.CannotConvertType, key.GetType(), "int")
- SR.Format(SR.CannotConvertType, typeof(CharacterMetrics)…
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/507349e52d38ed29.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/ValidationHelper.cs:44
// Throws an appropriate exception if a test fails.
internal static void VerifyPosition(ITextContainer tree, ITextPointer position)
{
VerifyPosition(tree, position, nameof(position));
}
// Verifies a TextPointer is non-null and is associated with a given TextContainer.
//
// Throws an appropriate exception if a test fails.
internal static void VerifyPosition(ITextContainer container, ITextPointer position, string paramName)
{
if (position == null)
{
throw new ArgumentNullException(paramName);
}
if (position.TextContainer != container)
{
throw new ArgumentException(SR.Format(SR.NotInAssociatedTree, paramName));
}
}
// Verifies two positions are safe to use as a logical text span.
//
// Throws ArgumentNullException if startPosition == null || endPosition == null
// ArgumentException if startPosition.TextContainer != endPosition.TextContainer or
// startPosition > endPosition
internal static void VerifyPositionPair(ITextPointer startPosition, ITextPointer endPosition)
{
ArgumentNullException.ThrowIfNull(startPosition);
ArgumentNullException.ThrowIfNull(endPosition);
if (startPosition.TextContainer != endPosition.TextContainer)
{
throw new ArgumentException(SR.Format(SR.InDifferentTextContainers, "startPosition", "endPosition"));
}
if (startPosition.CompareTo(endPosition) > 0)
{View on GitHub (pinned to 81131a70a4)