dotnet/wpf · error · ArgumentException

SR.RelativeSourceModeInvalid

Error message

SR.RelativeSourceModeInvalid

What it means

RelativeSource.InitializeMode validates that the Mode passed to a RelativeSource belongs to the set of supported modes (FindAncestor, Self, TemplatedParent, PreviousData, None). If the caller supplies a RelativeSourceMode value outside this set, the constructor (or the Mode setter, which also routes through InitializeMode) throws an ArgumentException with SR.RelativeSourceModeInvalid.

Solutions

  1. Use only defined RelativeSourceMode enum members: FindAncestor, Self, TemplatedParent, PreviousData, None.
  2. If converting from a string/int, use Enum.TryParse<RelativeSourceMode>(...) and check success before constructing.
  3. Verify persisted/bound mode values are in range before passing them to the RelativeSource constructor.

Example fix

// before
var rs = new RelativeSource((RelativeSourceMode)99);
// after
if (Enum.TryParse<RelativeSourceMode>(modeString, out var mode) &&
    mode is RelativeSourceMode.FindAncestor or RelativeSourceMode.Self
        or RelativeSourceMode.TemplatedParent or RelativeSourceMode.PreviousData
        or RelativeSourceMode.None)
{
    var rs = new RelativeSource(mode);
}
Defensive patterns

Strategy: validation

Validate before calling

bool isValidMode = Enum.IsDefined(typeof(RelativeSourceMode), mode);
if (!isValidMode) throw new ArgumentOutOfRangeException(nameof(mode));

Type guard

static bool IsValidRelativeSourceMode(object m) => m is RelativeSourceMode mode && Enum.IsDefined(typeof(RelativeSourceMode), mode);

Try / catch

try { var rs = new RelativeSource(mode); }
catch (ArgumentException ex) { log.Error("Invalid RelativeSourceMode: " + mode, ex); }

Prevention

When it happens

Trigger: Calling `new RelativeSource((RelativeSourceMode)someInt)` with an int cast that is not a defined enum value, or assigning an invalid RelativeSourceMode to the RelativeSource.Mode property.

Common situations: Deserializing a RelativeSource from config/XAML where an invalid numeric mode was persisted; hand-casting integers to the enum; reflection-based object construction passing bad mode values.

Understand the failure class

Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/RelativeSource.cs:289

        {
            Debug.Assert(IsUninitialized);

            if (mode == RelativeSourceMode.FindAncestor)
            {
                // default level
                _ancestorLevel = 1;
                _mode = mode;
            }
            else if (mode == RelativeSourceMode.PreviousData
                || mode == RelativeSourceMode.Self
                || mode == RelativeSourceMode.TemplatedParent)
            {
                _ancestorLevel = 0;
                _mode = mode;
            }
            else
            {
                throw new ArgumentException(SR.RelativeSourceModeInvalid, nameof(mode));
            }
        }
#endregion private methods

#region private fields

        private RelativeSourceMode _mode;
        private Type _ancestorType;
        private int _ancestorLevel = -1;    // while -1, indicates _mode has not been set

        private static RelativeSource s_previousData;
        private static RelativeSource s_templatedParent;
        private static RelativeSource s_self;
#endregion private fields
    }
}

View on GitHub (pinned to 81131a70a4)