dotnet/wpf · error · ArgumentException
SR.WrongSelectionType
Error message
SR.WrongSelectionType
What it means
TextSelectionProcessor.GenerateLocatorParts converts a selection into a ContentLocatorPart. After CheckSelection succeeds, it requires the resolved start pointer to be a TextPointer (the WPF text stack implementation); a different ITextPointer implementation cannot be used to compute element offsets and throws this ArgumentException.
Solutions
- Use anchors/selections created from WPF text content (FlowDocument, TextBox, RichTextBox) so the start pointer is a real TextPointer.
- Remove custom ITextPointer implementations from the anchor's backing container.
- Catch ArgumentException and skip locator-part generation for non-WPF-backed anchors.
Example fix
// before
var part = textSelectionProcessor.GenerateLocatorParts(anchor, documentPageView, startNode);
// after
if (anchor.Start is TextPointer)
{
var part = textSelectionProcessor.GenerateLocatorParts(anchor, documentPageView, startNode);
}
else
{
part = null; // not backed by the WPF text stack
} Defensive patterns
Strategy: validation
Validate before calling
bool wpfBacked = anchor.Start is TextPointer;
if (!wpfBacked) throw new InvalidOperationException("Anchor must be backed by WPF TextPointer"); Type guard
static bool HasWpfPointers(TextAnchor a) => a is { Start: TextPointer, End: TextPointer }; Try / catch
try { part = processor.GenerateLocatorParts(selection, node, pageView); }
catch (ArgumentException ex) when (ex.Message.Contains(nameof(selection))) { part = null; } Prevention
- Use only WPF text-stack content (FlowDocument, TextBox, RichTextBox) with the annotations service.
- Avoid custom ITextPointer implementations in annotation scenarios.
- Assert anchor.Start is TextPointer in debug builds.
When it happens
Trigger: Calling GenerateLocatorParts with a TextAnchor whose Start pointer is not a TextPointer — e.g. an anchor built over a custom/non-WPF ITextContainer or a mock/custom ITextPointer implementation.
Common situations: Custom text edit controls implementing ITextPointer but not backed by the WPF TextPointer class; unit tests injecting fake text pointers; mixing anchors created against other frameworks.
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.Format(SR.IncorrectLocatorPartType, " : ")
- SR.ParameterMustBeLogicalNode
- SR.WrongSelectionType
- SR.WrongSelectionType
- SR.WrongSelectionType
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/618fc848c30f837c.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Annotations/Anchoring/TextSelectionProcessor.cs:135
/// <param name="startNode">the node the locator parts should be in the
/// context of</param>
/// <returns>one or more locator parts representing the portion of 'startNode' spanned
/// by 'selection'</returns>
/// <exception cref="ArgumentNullException">startNode or selection is null</exception>
/// <exception cref="ArgumentException">selection is of the wrong type</exception>
public override IList<ContentLocatorPart>
GenerateLocatorParts(Object selection, DependencyObject startNode)
{
ArgumentNullException.ThrowIfNull(startNode);
ArgumentNullException.ThrowIfNull(selection);
ITextPointer start;
ITextPointer end;
IList<TextSegment> textSegments = null;
TextSelectionHelper.CheckSelection(selection, out start, out end, out textSegments);
if (!(start is TextPointer))
throw new ArgumentException(SR.WrongSelectionType, nameof(selection));
ITextPointer elementStart;
ITextPointer elementEnd;
// If we can't get the start/end of the node then we can't generate a locator part
if (!GetNodesStartAndEnd(startNode, out elementStart, out elementEnd))
return null;
if (elementStart.CompareTo(end) > 0)
throw new ArgumentException(SR.InvalidStartNodeForTextSelection, nameof(startNode));
if (elementEnd.CompareTo(start) < 0)
throw new ArgumentException(SR.InvalidStartNodeForTextSelection, nameof(startNode));
ContentLocatorPart part = new ContentLocatorPart(CharacterRangeElementName);
int startOffset = 0;
int endOffset = 0;View on GitHub (pinned to 81131a70a4)