dotnet/wpf · error · InvalidOperationException

SR.Illegal_InheritanceBehaviorSettor

Error message

SR.Illegal_InheritanceBehaviorSettor

What it means

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).

Solutions

  1. Set InheritanceBehavior in XAML or in the constructor before initialization completes
  2. Defer tree-level behavior changes until a new element is created rather than mutating an initialized one
  3. Wrap changes in Dispatcher.BeginInvoke at DispatcherPriority sent/Loaded only if you've verified the element is still uninitialized
  4. Restructure so resource lookup scope is controlled by the tree structure instead of late property changes

Example fix

// before
myControl.Loaded += (s,e) => myControl.InheritanceBehavior = InheritanceBehavior.SkipToAppNow;
// after
public MyControl() { InitializeComponent(); InheritanceBehavior = InheritanceBehavior.SkipToAppNow; }
Defensive patterns

Strategy: validation

Validate before calling

if (fe.IsInitialized) throw new InvalidOperationException("InheritanceBehavior can only be set before initialization");

Type guard

bool CanSetInheritanceBehavior(FrameworkElement fe) => !fe.IsInitialized;

Try / catch

try { fe.InheritanceBehavior = InheritanceBehavior.SkipToAppNow; }
catch (InvalidOperationException) { /* element already initialized; set at construction instead */ }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/61fd34e23efa0a94. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/FrameworkElement.cs:2650

                    _flags = (InternalFlags)((inheritanceBehavior & inheritanceBehaviorMask) | (((uint)_flags) & ~inheritanceBehaviorMask));

                    if (_parent != null)
                    {
                        // This means that we are in the process of xaml parsing:
                        // an instance of FE has been created and added to a parent,
                        // but no children yet added to it (otherwise it would be initialized already
                        // and we would not be allowed to change InheritanceBehavior).
                        // So we need to re-calculate properties accounting for the new
                        // inheritance behavior.
                        // This must have no performance effect as the subtree of this
                        // element is empty (no children yet added).
                        TreeWalkHelper.InvalidateOnTreeChange(/*fe:*/this, /*fce:*/null, _parent, true);
                    }
                }
                else
                {
                    throw new InvalidOperationException(SR.Illegal_InheritanceBehaviorSettor);
                }
            }
        }


        #region Data binding

        /// <summary>
        /// Add / Remove TargetUpdatedEvent handler
        /// </summary>
        public event EventHandler<DataTransferEventArgs> TargetUpdated
        {
            add     { AddHandler(Binding.TargetUpdatedEvent, value); }
            remove  { RemoveHandler(Binding.TargetUpdatedEvent, value); }
        }


        /// <summary>

View on GitHub (pinned to 81131a70a4)