{"record":{"id":"95f5643fe6dff5f3","repo":"dotnet/wpf","slug":"sr-relativesourceneedsmode","errorCode":null,"errorMessage":"SR.RelativeSourceNeedsMode","messagePattern":"SR\\.RelativeSourceNeedsMode","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/RelativeSource.cs","lineNumber":85,"sourceCode":"            InitializeMode(mode);\n            AncestorType = ancestorType;\n            AncestorLevel = ancestorLevel;\n        }\n\n#endregion constructors\n\n#region ISupportInitialize\n\n        /// <summary>Begin Initialization</summary>\n        void ISupportInitialize.BeginInit()\n        {\n        }\n\n        /// <summary>End Initialization, verify that internal state is consistent</summary>\n        void ISupportInitialize.EndInit()\n        {\n            if (IsUninitialized)\n                throw new InvalidOperationException(SR.RelativeSourceNeedsMode);\n            if (_mode == RelativeSourceMode.FindAncestor && (AncestorType == null))\n                throw new InvalidOperationException(SR.RelativeSourceNeedsAncestorType);\n        }\n\n#endregion ISupportInitialize\n\n#region public properties\n\n        /// <summary>static instance of RelativeSource for PreviousData mode.\n        /// </summary>\n        public static RelativeSource PreviousData\n        {\n            get\n            {\n                if (s_previousData == null)\n                {\n                    s_previousData = new RelativeSource(RelativeSourceMode.PreviousData);\n                }","sourceCodeStart":67,"sourceCodeEnd":103,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/RelativeSource.cs#L67-L103","documentation":"RelativeSource supports ISupportInitialize for XAML construction. EndInit validates the deferred state: if initialization ended with Mode still unset (IsUninitialized), it throws InvalidOperationException because a RelativeSource without a mode is unusable.","triggerScenarios":"Completing ISupportInitialize on a RelativeSource created without setting Mode (e.g. XAML <RelativeSource> with no Mode attribute, then EndInit).","commonSituations":"XAML where the Mode attribute is misspelled or omitted on a RelativeSource element; deserialization pipelines calling EndInit without setting Mode; constructing RelativeSource via default ctor in markup extensions.","solutions":["Set Mode on the RelativeSource (FindAncestor, TemplatedParent, Self, PreviousData, FindAncestor)","Use the RelativeSource(mode) constructor instead of the parameterless one in code","Fix the XAML so Mode is present and correctly cased"],"exampleFix":"<!-- before -->\n<RelativeSource AncestorType=\"Window\"/>\n<!-- after -->\n<RelativeSource Mode=\"FindAncestor\" AncestorType=\"Window\"/>","handlingStrategy":"validation","validationCode":"var rs = new RelativeSource();\n// ... set properties from config ...\nif (rs.Mode == RelativeSourceMode.FindAncestor && rs.AncestorType == null)\n    rs.AncestorType = typeof(Window);\nrs.EndInit();","typeGuard":null,"tryCatchPattern":"try { rs.EndInit(); }\ncatch (InvalidOperationException ex) { /* Mode missing — supply defaults */ }","preventionTips":["Always specify Mode on RelativeSource elements","Prefer the RelativeSource(mode) constructor in code","Validate XAML attribute casing of Mode"],"tags":["wpf","data-binding","relativesource","xaml"],"backgroundTag":"missing-required-argument","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"}