dotnet/wpf · error · InvalidOperationException

SR.Illegal_InheritanceBehaviorSettor

Error message

SR.Illegal_InheritanceBehaviorSettor

What it means

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.

Solutions

  1. Set InheritanceBehavior before initialization: in the element constructor or inside BeginInit/EndInit window
  2. Set it in XAML on the element declaration so it applies during parsing before initialization completes
  3. If a late change is required, recreate/re-parse the element with the desired behavior

Example fix

// before
myElement.Loaded += (s, e) =>
{
    myElement.InheritanceBehavior = InheritanceBehavior.SkipToAppNow; // throws: already initialized
};

// after
public MyElement() // or set in XAML before init completes
{
    InheritanceBehavior = InheritanceBehavior.SkipToAppNow;
}
Defensive patterns

Strategy: validation

Validate before calling

if (element.IsInitialized)
    throw new InvalidOperationException("InheritanceBehavior cannot be set after the element is initialized");
element.InheritanceBehavior = InheritanceBehavior.SkipToAppNow;

Type guard

static bool CanSetInheritanceBehavior(FrameworkContentElement e) => !e.IsInitialized;

Try / catch

try { element.InheritanceBehavior = value; }
catch (InvalidOperationException ex) { log.Error("Set InheritanceBehavior before initialization", ex); }

Prevention

When it happens

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

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

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/6563ad953a112267. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/FrameworkContentElement.cs:1512

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

                    if (_parent != null)
                    {
                        // This means that we are in the process of xaml parsing:
                        // an instance of FCE 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:*/null, /*fce:*/this, _parent, true);
                    }
                }
                else
                {
                    throw new InvalidOperationException(SR.Illegal_InheritanceBehaviorSettor);
                }
            }
        }

        #endregion LogicalTree

        #region ISupportInitialize

        /// <summary>
        ///     Initialization of this element is about to begin
        /// </summary>
        public virtual void BeginInit()
        {
            // Nested BeginInits on the same instance aren't permitted
            if (ReadInternalFlag(InternalFlags.InitPending))
            {
                throw new InvalidOperationException(SR.NestedBeginInitNotSupported);
            }

View on GitHub (pinned to 81131a70a4)