dotnet/wpf · error · ArgumentException
SR.WrongSelectionType
Error message
SR.WrongSelectionType (anchor1: type = {anchor1.GetType()}) What it means
TextSelectionHelper.MergeSelections merges two annotation anchors into one. The anchor1 parameter must be either null or a TextAnchor; any other type throws this ArgumentException because only TextAnchor instances can be merged.
Solutions
- Pass a TextAnchor instance (or null) as anchor1; cast or convert the selection before calling.
- Check anchor1 is TextAnchor before calling; if it is a TextRange, convert it via the appropriate selection processor first.
- Catch ArgumentException and treat the merge as failed, returning a fallback anchor.
Example fix
// before
object merged;
TextSelectionHelper.MergeSelections(myTextRange, existingAnchor, out merged);
// after
if (myTextRange is TextAnchor first)
{
object merged;
TextSelectionHelper.MergeSelections(first, existingAnchor, out merged);
}
else
{
merged = existingAnchor; // skip incompatible anchor
} Defensive patterns
Strategy: type-guard
Validate before calling
if (anchor1 != null && !(anchor1 is TextAnchor)) throw new InvalidOperationException("anchor1 must be TextAnchor"); Type guard
static bool IsMergeableAnchor(object a) => a == null || a is TextAnchor;
Try / catch
try { ok = TextSelectionHelper.MergeSelections(a1, a2, out merged); }
catch (ArgumentException ex) when (ex.ParamName == null && ex.Message.StartsWith("anchor1")) { merged = a2; ok = merged != null; } Prevention
- Store anchors in strongly typed collections (List<TextAnchor>) instead of object.
- Never pass raw TextSelection/TextRange objects where TextAnchor is expected.
- Convert selections to TextAnchor via their selection processor at creation time.
When it happens
Trigger: Calling TextSelectionHelper.MergeSelections with an anchor1 that is non-null but not a TextAnchor — e.g. passing a TextRange, TextSelection, ContentLocator or a custom selection object as the first anchor.
Common situations: Storing anchors as object and losing track of their concrete type; mixing anchor types produced by different selection processors; deserializing anchors whose type got changed between versions.
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/fbac92859939dded.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Annotations/Anchoring/TextSelectionHelper.cs:72
#region Public Methods
/// <summary>
/// Merges the two anchors into one, if possible.
/// </summary>
/// <param name="anchor1">anchor to merge </param>
/// <param name="anchor2">other anchor to merge </param>
/// <param name="newAnchor">new anchor that contains the data from both
/// anchor1 and anchor2</param>
/// <returns>true if the anchors were merged, false otherwise
/// </returns>
/// <exception cref="ArgumentNullException">anchor1 or anchor2 are null</exception>
public static bool MergeSelections(Object anchor1, Object anchor2, out Object newAnchor)
{
TextAnchor firstAnchor = anchor1 as TextAnchor;
TextAnchor secondAnchor = anchor2 as TextAnchor;
if ((anchor1 != null) && (firstAnchor == null))
throw new ArgumentException(SR.WrongSelectionType, $"anchor1: type = {anchor1.GetType()}");
if ((anchor2 != null) && (secondAnchor == null))
throw new ArgumentException(SR.WrongSelectionType, $"Anchor2: type = {anchor2.GetType()}");
if (firstAnchor == null)
{
newAnchor = secondAnchor;
return newAnchor != null;
}
if (secondAnchor == null)
{
newAnchor = firstAnchor;
return newAnchor != null;
}
newAnchor = TextAnchor.ExclusiveUnion(firstAnchor, secondAnchor);
View on GitHub (pinned to 81131a70a4)