{"record":{"id":"e426f3493625fa81","repo":"dotnet/wpf","slug":"sr-relativesourceneedsancestortype","errorCode":null,"errorMessage":"SR.RelativeSourceNeedsAncestorType","messagePattern":"SR\\.RelativeSourceNeedsAncestorType","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/RelativeSource.cs","lineNumber":87,"sourceCode":"            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                }\n                return s_previousData;\n            }","sourceCodeStart":69,"sourceCodeEnd":105,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/RelativeSource.cs#L69-L105","documentation":"EndInit validation for RelativeSource also requires that FindAncestor mode specifies an AncestorType. A RelativeSource in FindAncestor mode with a null AncestorType throws InvalidOperationException — the engine would not know which ancestor type to walk to.","triggerScenarios":"Calling ISupportInitialize.EndInit when _mode == RelativeSourceMode.FindAncestor and AncestorType is null.","commonSituations":"XAML <RelativeSource Mode=\"FindAncestor\" AncestorLevel=\"1\"/> missing AncestorType; code building a FindAncestor RelativeSource but forgetting the type.","solutions":["Set AncestorType (e.g. typeof(Window)) when using FindAncestor mode","Add AncestorType= in the XAML element","Switch to a mode that does not need AncestorType if ancestor lookup is not intended"],"exampleFix":"// before\nvar rs = new RelativeSource(RelativeSourceMode.FindAncestor) { AncestorLevel = 1 }; // EndInit throws\n// after\nvar rs = new RelativeSource(RelativeSourceMode.FindAncestor) { AncestorLevel = 1, AncestorType = typeof(Window) };","handlingStrategy":"validation","validationCode":"if (rs.Mode == RelativeSourceMode.FindAncestor && rs.AncestorType == null)\n    throw new InvalidOperationException(\"FindAncestor requires AncestorType\");","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Pair AncestorLevel with AncestorType in FindAncestor mode","Add AncestorType in XAML whenever Mode=\"FindAncestor\"","Use x:Static/type references carefully so AncestorType resolves"],"tags":["wpf","data-binding","relativesource"],"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"}