{"record":{"id":"a6f7652d6f8243fa","repo":"dotnet/wpf","slug":"sr-relativesourcemodeinvalid","errorCode":null,"errorMessage":"SR.RelativeSourceModeInvalid","messagePattern":"SR\\.RelativeSourceModeInvalid","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/RelativeSource.cs","lineNumber":289,"sourceCode":"        {\n            Debug.Assert(IsUninitialized);\n\n            if (mode == RelativeSourceMode.FindAncestor)\n            {\n                // default level\n                _ancestorLevel = 1;\n                _mode = mode;\n            }\n            else if (mode == RelativeSourceMode.PreviousData\n                || mode == RelativeSourceMode.Self\n                || mode == RelativeSourceMode.TemplatedParent)\n            {\n                _ancestorLevel = 0;\n                _mode = mode;\n            }\n            else\n            {\n                throw new ArgumentException(SR.RelativeSourceModeInvalid, nameof(mode));\n            }\n        }\n#endregion private methods\n\n#region private fields\n\n        private RelativeSourceMode _mode;\n        private Type _ancestorType;\n        private int _ancestorLevel = -1;    // while -1, indicates _mode has not been set\n\n        private static RelativeSource s_previousData;\n        private static RelativeSource s_templatedParent;\n        private static RelativeSource s_self;\n#endregion private fields\n    }\n}\n","sourceCodeStart":271,"sourceCodeEnd":306,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/RelativeSource.cs#L271-L306","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Use only defined RelativeSourceMode enum members: FindAncestor, Self, TemplatedParent, PreviousData, None.","If converting from a string/int, use Enum.TryParse<RelativeSourceMode>(...) and check success before constructing.","Verify persisted/bound mode values are in range before passing them to the RelativeSource constructor."],"exampleFix":"// before\nvar rs = new RelativeSource((RelativeSourceMode)99);\n// after\nif (Enum.TryParse<RelativeSourceMode>(modeString, out var mode) &&\n    mode is RelativeSourceMode.FindAncestor or RelativeSourceMode.Self\n        or RelativeSourceMode.TemplatedParent or RelativeSourceMode.PreviousData\n        or RelativeSourceMode.None)\n{\n    var rs = new RelativeSource(mode);\n}","handlingStrategy":"validation","validationCode":"bool isValidMode = Enum.IsDefined(typeof(RelativeSourceMode), mode);\nif (!isValidMode) throw new ArgumentOutOfRangeException(nameof(mode));","typeGuard":"static bool IsValidRelativeSourceMode(object m) => m is RelativeSourceMode mode && Enum.IsDefined(typeof(RelativeSourceMode), mode);","tryCatchPattern":"try { var rs = new RelativeSource(mode); }\ncatch (ArgumentException ex) { log.Error(\"Invalid RelativeSourceMode: \" + mode, ex); }","preventionTips":["Never cast arbitrary ints to RelativeSourceMode without Enum.IsDefined.","Parse mode strings with Enum.TryParse and validate the result.","Keep mode values in strongly typed config rather than raw numbers."],"tags":["wpf","data-binding","enum","argument"],"backgroundTag":"invalid-enum-value","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}