{"record":{"id":"22541402a1cab600","repo":"dotnet/wpf","slug":"sr-relativesourcenotinfindancestormode","errorCode":null,"errorMessage":"SR.RelativeSourceNotInFindAncestorMode","messagePattern":"SR\\.RelativeSourceNotInFindAncestorMode","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/RelativeSource.cs","lineNumber":178,"sourceCode":"        /// </summary>\n        /// <remarks> if Mode has not been set explicitly, setting AncestorType will implicitly lock Mode to FindAncestor. </remarks>\n        /// <exception cref=\"InvalidOperationException\"> RelativeSource is not in FindAncestor mode </exception>\n        public Type AncestorType\n        {\n            get { return _ancestorType; }\n            set\n            {\n                if (IsUninitialized)\n                {\n                    Debug.Assert(_mode == RelativeSourceMode.FindAncestor);\n                    AncestorLevel = 1;  // lock the mode and set default level\n                }\n\n                if (_mode != RelativeSourceMode.FindAncestor)\n                {\n                    // in all other modes, AncestorType should not get set to a non-null value\n                    if (value != null)\n                        throw new InvalidOperationException(SR.RelativeSourceNotInFindAncestorMode);\n                }\n                else\n                {\n                    _ancestorType = value;\n                }\n            }\n        }\n\n        /// <summary>\n        /// This method is used by TypeDescriptor to determine if this property should\n        /// be serialized.\n        /// </summary>\n        [EditorBrowsable(EditorBrowsableState.Never)]\n        public bool ShouldSerializeAncestorType()\n        {\n            return (_mode == RelativeSourceMode.FindAncestor);\n        }\n","sourceCodeStart":160,"sourceCodeEnd":196,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/RelativeSource.cs#L160-L196","documentation":"Setting AncestorType on a RelativeSource whose Mode is not FindAncestor throws this InvalidOperationException. AncestorType is only meaningful in FindAncestor mode; once Mode has been set explicitly to another value (e.g. PreviousData, TemplatedParent, Self), assigning AncestorType is an invalid configuration, so the setter rejects it rather than silently ignoring the type.","triggerScenarios":"Assigning RelativeSource.AncestorType != null when _mode is Self, TemplatedParent, or PreviousData.","commonSituations":"Copy-pasted XAML keeping an AncestorType attribute while changing Mode to Self/TemplatedParent; code that sets AncestorType before Mode and had Mode resolved to something other than FindAncestor.","solutions":["Set Mode=\"FindAncestor\" or remove the AncestorType attribute","Set AncestorType only after Mode is FindAncestor","Delete leftover AncestorType/AncestorLevel attributes in non-FindAncestor RelativeSource elements"],"exampleFix":"<!-- before -->\n<RelativeSource Mode=\"Self\" AncestorType=\"Window\"/>\n<!-- after -->\n<RelativeSource Mode=\"FindAncestor\" AncestorType=\"Window\"/>","handlingStrategy":"validation","validationCode":"if (rs.Mode != RelativeSourceMode.FindAncestor)\n    rs.AncestorType = null; // clear before changing mode","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Remove AncestorType/AncestorLevel attributes when Mode is not FindAncestor","Set Mode first, then ancestor properties"],"tags":["wpf","data-binding","relativesource","xaml"],"backgroundTag":"invalid-argument-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"}