dotnet/wpf · error · ArgumentException
SR.WrongSelectionType
Error message
SR.WrongSelectionType (selection: type={selection.GetType()}) What it means
CheckSelection validates that the selection object handed to the processor is a TextAnchor or TextSelection whose start TextContainer is a FixedTextContainer or DocumentSequenceTextContainer. Otherwise it throws ArgumentException(SR.WrongSelectionType) with the actual runtime type. Called from GenerateLocatorParts and textSegments.
Solutions
- Only pass selections created from DocumentPageView/FixedPage content (FixedTextContainer).
- Use the TextSelectionProcessor subclass that matches the content type (Fixed vs tree).
- Check selection is ITextSelection/TextAnchor and its TextContainer type before calling.
Example fix
// before
processor.GenerateLocatorParts(unknownSelection); // may be a flow selection
// after
if (sel is TextSelection ts && (ts.Start.TextContainer is FixedTextContainer || ts.Start.TextContainer is DocumentSequenceTextContainer))
processor.GenerateLocatorParts(ts); Defensive patterns
Strategy: type-guard
Validate before calling
if (!(selection is TextSelection || selection is TextAnchor)) throw new InvalidOperationException("selection must be TextSelection or TextAnchor"); Type guard
bool IsFixedSelection(object s) => s is TextSelection ts && (ts.Start.TextContainer is FixedTextContainer || ts.Start.TextContainer is DocumentSequenceTextContainer);
Try / catch
try { processor.GenerateLocatorParts(selection); } catch (ArgumentException ex) { /* wrong selection type — re-derive from page view */ } Prevention
- Only anchor selections made in fixed-document viewers
- Check TextContainer type before calling the processor
- Use the matching SelectionProcessor for the content type
When it happens
Trigger: Calling GenerateLocatorParts (or the textSegments helper) with a selection that is neither TextAnchor nor TextSelection (first throw), or whose Start.TextContainer is not fixed/sequence content (second throw at line 449 is the container check).
Common situations: Passing a TextSelection from a FlowDocumentReader to a fixed-document annotation service; passing arbitrary objects (e.g. a TextPointer) as 'selection'; mixing flow and fixed anchoring processors.
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.EmptySelectionNotSupported
- Unsupported Selection
- anchorLocator.Parts
- annotation component
- Can only call SelectAll when CanSelectMultipleItems is true.
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/510dc454c67689c9.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Annotations/Anchoring/FixedTextSelectionProcessor.cs:449
ITextPointer start = null;
ITextRange textRange = selection as ITextRange;
if (textRange != null)
{
start = textRange.Start;
textSegments = textRange.TextSegments;
}
else
{
TextAnchor anchor = selection as TextAnchor;
if (anchor != null)
{
start = anchor.Start;
textSegments = anchor.TextSegments;
}
else
{
throw new ArgumentException(SR.WrongSelectionType, $"selection: type={selection.GetType()}");
}
}
if (!(start.TextContainer is FixedTextContainer ||
start.TextContainer is DocumentSequenceTextContainer))
throw new ArgumentException(SR.WrongSelectionType, $"selection: type={selection.GetType()}");
return textSegments;
}
/// <summary>
/// Checks if the selection object satisfies the requirements
/// for this processor
/// </summary>
/// <param name="selection">selection</param>
/// <returns>ITextRange interface, implemented by the object</returns>
private TextAnchor CheckAnchor(object selection)
{View on GitHub (pinned to 81131a70a4)