dotnet/wpf · error · ArgumentException

SR.WrongSelectionType

Error message

SR.WrongSelectionType

What it means

TextSelectionHelper.GetAnchorPoint computes the anchor (start) point of an annotation selection. It only understands TextAnchor objects; passing any other selection type throws this ArgumentException.

Solutions

  1. Pass a TextAnchor instance; cast the selection before calling.
  2. If you have a TextRange, resolve it through TextSelectionProcessor to obtain a TextAnchor first.
  3. Guard with a type check and skip GetAnchorPoint for non-TextAnchor selections.
  4. Catch ArgumentException around GetAnchorPoint and return a default/null anchor point.

Example fix

// before
var point = TextSelectionHelper.GetAnchorPoint(mySelection);
// after
var point = mySelection is TextAnchor anchor
    ? TextSelectionHelper.GetAnchorPoint(anchor)
    : (Point?)null;
Defensive patterns

Strategy: type-guard

Validate before calling

if (!(selection is TextAnchor)) throw new InvalidOperationException("GetAnchorPoint requires TextAnchor");

Type guard

static bool IsTextAnchor(object s) => s is TextAnchor;

Try / catch

try { var p = TextSelectionHelper.GetAnchorPoint(selection); }
catch (ArgumentException ex) when (ex.ParamName == nameof(selection)) { p = null; }

Prevention

When it happens

Trigger: Calling TextSelectionHelper.GetAnchorPoint(selection) with something that is not a TextAnchor — e.g. a TextRange, TextSelection, or arbitrary object.

Common situations: Retrieving an anchor from a store/dictionary typed as object and forgetting its concrete type; passing the raw editor selection instead of the framework-produced TextAnchor.

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


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Annotations/Anchoring/TextSelectionHelper.cs:228

        /// <summary>
        ///  Gets the anchor point for the selection
        /// </summary>
        /// <param name="selection">the anchor to examine</param>
        /// <returns>
        /// The anchor point of the selection; 
        /// If there is no valid AnchorPoint returns Point(Double.NaN, Double.NaN). 
        /// </returns>
        /// <exception cref="ArgumentNullException">anchor is null</exception>
        /// <exception cref="ArgumentException">anchor is of wrong type</exception>
        public static Point GetAnchorPoint(Object selection)
        {
            ArgumentNullException.ThrowIfNull(selection);

            TextAnchor anchor = selection as TextAnchor;

            if (anchor == null)
                throw new ArgumentException(SR.WrongSelectionType, nameof(selection));

            return GetAnchorPointForPointer(anchor.Start.CreatePointer(LogicalDirection.Forward));
        }

        /// <summary>
        ///  Gets the anchor point for the text pointer
        /// </summary>
        /// <param name="pointer">the pointer to examine</param>
        /// <returns>
        /// The anchor point of the text pointer
        /// </returns>
        /// <exception cref="ArgumentNullException">pointer is null</exception>
        public static Point GetAnchorPointForPointer(ITextPointer pointer)
        {
            ArgumentNullException.ThrowIfNull(pointer);

            Rect rect = GetAnchorRectangle(pointer);

View on GitHub (pinned to 81131a70a4)