{"record":{"id":"6563ad953a112267","repo":"dotnet/wpf","slug":"sr-illegal-inheritancebehaviorsettor","errorCode":null,"errorMessage":"SR.Illegal_InheritanceBehaviorSettor","messagePattern":"SR\\.Illegal_InheritanceBehaviorSettor","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/FrameworkContentElement.cs","lineNumber":1512,"sourceCode":"\n                    _flags = (InternalFlags)((inheritanceBehavior & inheritanceBehaviorMask) | (((uint)_flags) & ~inheritanceBehaviorMask));\n\n                    if (_parent != null)\n                    {\n                        // This means that we are in the process of xaml parsing:\n                        // an instance of FCE has been created and added to a parent,\n                        // but no children yet added to it (otherwise it would be initialized already\n                        // and we would not be allowed to change InheritanceBehavior).\n                        // So we need to re-calculate properties accounting for the new\n                        // inheritance behavior.\n                        // This must have no performance effect as the subtree of this\n                        // element is empty (no children yet added).\n                        TreeWalkHelper.InvalidateOnTreeChange(/*fe:*/null, /*fce:*/this, _parent, true);\n                    }\n                }\n                else\n                {\n                    throw new InvalidOperationException(SR.Illegal_InheritanceBehaviorSettor);\n                }\n            }\n        }\n\n        #endregion LogicalTree\n\n        #region ISupportInitialize\n\n        /// <summary>\n        ///     Initialization of this element is about to begin\n        /// </summary>\n        public virtual void BeginInit()\n        {\n            // Nested BeginInits on the same instance aren't permitted\n            if (ReadInternalFlag(InternalFlags.InitPending))\n            {\n                throw new InvalidOperationException(SR.NestedBeginInitNotSupported);\n            }","sourceCodeStart":1494,"sourceCodeEnd":1530,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/FrameworkContentElement.cs#L1494-L1530","documentation":"Setting FrameworkContentElement.InheritanceBehavior is only legal while the element is being initialized (not yet initialized, still pending init). Assigning it after the element is initialized throws InvalidOperationException with SR.Illegal_InheritanceBehaviorSettor, because changing inheritance behavior mid-tree would leave property inheritance state inconsistent.","triggerScenarios":"Assigning element.InheritanceBehavior after the element's EndInit has run (IsInitialized == true) - i.e., after the element has been loaded into the tree and initialized.","commonSituations":"Changing InheritanceBehavior in a Loaded handler or after DataContext/templating completes; setting it programmatically on an element that has already been added to a parsed/loaded logical tree.","solutions":["Set InheritanceBehavior before initialization: in the element constructor or inside BeginInit/EndInit window","Set it in XAML on the element declaration so it applies during parsing before initialization completes","If a late change is required, recreate/re-parse the element with the desired behavior"],"exampleFix":"// before\nmyElement.Loaded += (s, e) =>\n{\n    myElement.InheritanceBehavior = InheritanceBehavior.SkipToAppNow; // throws: already initialized\n};\n\n// after\npublic MyElement() // or set in XAML before init completes\n{\n    InheritanceBehavior = InheritanceBehavior.SkipToAppNow;\n}","handlingStrategy":"validation","validationCode":"if (element.IsInitialized)\n    throw new InvalidOperationException(\"InheritanceBehavior cannot be set after the element is initialized\");\nelement.InheritanceBehavior = InheritanceBehavior.SkipToAppNow;","typeGuard":"static bool CanSetInheritanceBehavior(FrameworkContentElement e) => !e.IsInitialized;","tryCatchPattern":"try { element.InheritanceBehavior = value; }\ncatch (InvalidOperationException ex) { log.Error(\"Set InheritanceBehavior before initialization\", ex); }","preventionTips":["Set InheritanceBehavior in the constructor or in XAML at declaration time","Never change it in Loaded/Initialized handlers","Recreate the element if a post-init change is needed"],"tags":["wpf","invalid-state-transition","property-inheritance"],"backgroundTag":"invalid-state-transition","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"}