dotnet/wpf · error · ArgumentException

SR.Format(SR.InDifferentScope, "start", "end")

Error message

SR.Format(SR.InDifferentScope, "start", "end")

What it means

TextElement.Reposition requires start and end pointers to be equally scoped, i.e. contained by the same tree node. When their containing nodes differ it throws ArgumentException with the InDifferentScope message naming 'start' and 'end'. This prevents repositioning an element with a range that crosses element boundaries.

Solutions

  1. Ensure start and end are within the same containing element before calling Reposition.
  2. Split or clamp the range so both endpoints share a scope.
  3. Use a TextRange/selection API that handles cross-element ranges instead of Reposition directly.

Example fix

// before
element.Reposition(startPointerInP1, endPointerInP2);
// after
if (startPointerInP1.Parent == endPointerInP2.Parent)
    element.Reposition(startPointerInP1, endPointerInP2);
Defensive patterns

Strategy: validation

Validate before calling

bool sameScope = start == null || start.GetContainingNode() == end.GetContainingNode();
if (!sameScope) throw new ArgumentException("start/end must share scope");

Try / catch

try { element.Reposition(start, end); }
catch (ArgumentException ex) when (ex.Message.Contains("different scope")) { /* split the range and reposition per scope */ }

Prevention

When it happens

Trigger: Calling Reposition with a start/end pair whose GetContainingNode() values differ (e.g. start in one Paragraph, end in another).

Common situations: Selection ranges spanning multiple paragraphs passed to element repositioning; paste/merge operations in FlowDocument where range endpoints land in different scopes.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/d5d44519715786bf. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/TextElement.cs:123

                // start/end must be equally scoped.  But we want to discount
                // this TextElement when considering scoping -- it will be
                // extracted before the final insert.

                SplayTreeNode startNode = start.GetScopingNode();
                SplayTreeNode endNode = end.GetScopingNode();

                if (startNode == _textElementNode)
                {
                    startNode = _textElementNode.GetContainingNode();
                }
                if (endNode == _textElementNode)
                {
                    endNode = _textElementNode.GetContainingNode();
                }

                if (startNode != endNode)
                {
                    throw new ArgumentException(SR.Format(SR.InDifferentScope, "start", "end"));
                }
            }

            if (this.IsInTree)
            {
                tree = EnsureTextContainer();

                if (start == null)
                {
                    //
                    // Case 0: Extract this element from its tree.
                    //

                    tree.BeginChange();
                    try
                    {
                        tree.ExtractElementInternal(this);
                    }

View on GitHub (pinned to 81131a70a4)