{"record":{"id":"61fd34e23efa0a94","repo":"dotnet/wpf","slug":"sr-illegal-inheritancebehaviorsettor-frameworkelement","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/FrameworkElement.cs","lineNumber":2650,"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 FE 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:*/this, /*fce:*/null, _parent, true);\n                    }\n                }\n                else\n                {\n                    throw new InvalidOperationException(SR.Illegal_InheritanceBehaviorSettor);\n                }\n            }\n        }\n\n\n        #region Data binding\n\n        /// <summary>\n        /// Add / Remove TargetUpdatedEvent handler\n        /// </summary>\n        public event EventHandler<DataTransferEventArgs> TargetUpdated\n        {\n            add     { AddHandler(Binding.TargetUpdatedEvent, value); }\n            remove  { RemoveHandler(Binding.TargetUpdatedEvent, value); }\n        }\n\n\n        /// <summary>","sourceCodeStart":2632,"sourceCodeEnd":2668,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/FrameworkElement.cs#L2632-L2668","documentation":"InheritanceBehavior can only be changed while the element is not yet initialized (or in the allowed initialized states per the setter's conditions); otherwise the inheritance walk cannot be invalidated correctly. When the setter is called in a disallowed state, WPF throws InvalidOperationException(SR.Illegal_InheritanceBehaviorSettor).","triggerScenarios":"Setting InheritanceBehavior on an element after it has been initialized (EndInit/EndInit reached, tree-connected state) where the setter's internal conditions are not met.","commonSituations":"Changing InheritanceBehavior in a Loaded handler or after the window is shown; toggling it in code-behind after XAML initialization; frameworks that re-parent initialized elements.","solutions":["Set InheritanceBehavior in XAML or in the constructor before initialization completes","Defer tree-level behavior changes until a new element is created rather than mutating an initialized one","Wrap changes in Dispatcher.BeginInvoke at DispatcherPriority sent/Loaded only if you've verified the element is still uninitialized","Restructure so resource lookup scope is controlled by the tree structure instead of late property changes"],"exampleFix":"// before\nmyControl.Loaded += (s,e) => myControl.InheritanceBehavior = InheritanceBehavior.SkipToAppNow;\n// after\npublic MyControl() { InitializeComponent(); InheritanceBehavior = InheritanceBehavior.SkipToAppNow; }","handlingStrategy":"validation","validationCode":"if (fe.IsInitialized) throw new InvalidOperationException(\"InheritanceBehavior can only be set before initialization\");","typeGuard":"bool CanSetInheritanceBehavior(FrameworkElement fe) => !fe.IsInitialized;","tryCatchPattern":"try { fe.InheritanceBehavior = InheritanceBehavior.SkipToAppNow; }\ncatch (InvalidOperationException) { /* element already initialized; set at construction instead */ }","preventionTips":["Set InheritanceBehavior in constructor or XAML only","Do not change it in Loaded/event handlers","Design resource-scope needs into tree structure up front"],"tags":["wpf","invalid-operation","initialization"],"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-22T01:17:13.364Z"}