dotnet/wpf · error · InvalidOperationException

SR.Format(SR.Invalid_IInputElement, doTarget.GetType())

Error message

SR.Format(SR.Invalid_IInputElement, doTarget.GetType())

What it means

UpdateSourceOfElement raises the SourceChanged routed event on the target element and switches over UIElement/UIElement3D (and ContentElement above). If the target is none of these, an InvalidOperationException is thrown since the event cannot be raised on the element - an internal invariant guard.

Solutions

  1. Ensure the visual/root elements of a PresentationSource are UIElement/ContentElement/UIElement3D derived.
  2. Fix host/interop code that inserts non-element DependencyObjects into the WPF tree.
  3. Unify WPF assembly versions to avoid type identity mismatches.
  4. Capture a stack trace of the ancestor-changed path to identify the offending element type in the message.

Example fix

// before
presentationSource.RootVisual = (Visual)foreignObject; // not a UIElement

// after
presentationSource.RootVisual = new RootVisualHost(); // derives from UIElement
Defensive patterns

Strategy: type-guard

Validate before calling

if (!(doTarget is UIElement || doTarget is ContentElement || doTarget is UIElement3D))
    throw new InvalidOperationException("PresentationSource root must be a recognized input element");

Type guard

static bool CanRaiseSourceChanged(DependencyObject d) =>
    d is UIElement || d is ContentElement || d is UIElement3D;

Prevention

When it happens

Trigger: A PresentationSource's root/element changing in a way that assigns a target element which is not a recognized input element - typically via OnAncestorChanged, RootChanged, or OnVisualAncestorChanged callbacks with nonstandard elements in the tree.

Common situations: Custom visual trees containing foreign DependencyObjects presented as element roots; interop hosts swapping in non-WPF roots; assembly version mismatches during upgrades.

Understand the failure class

Background: "This is a bug, please report it": internal invariant violations, unreachable panics, and SNH errors explained — this error's family across 47 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/PresentationSource.cs:699

                SourceChangedEventArgs args = new SourceChangedEventArgs(cachedSource, realSource)
                {
                    RoutedEvent = SourceChangedEvent
                };
                if (doTarget is UIElement uiElement)
                {
                    uiElement.RaiseEvent(args);
                }                
                else if (doTarget is ContentElement contentElement)
                {
                    contentElement.RaiseEvent(args);
                }
                else if (doTarget is UIElement3D uiElement3D)
                {
                    uiElement3D.RaiseEvent(args);
                }
                else
                {
                    throw new InvalidOperationException(SR.Format(SR.Invalid_IInputElement, doTarget.GetType())); 
                }

                calledOut = true;
            }

            return calledOut;
        }

        #endregion

        //------------------------------------------------------
        //
        //  Private Static Members
        //
        //------------------------------------------------------  

        #region Private Static Members
        // We use a private DP for the RootSource (the connection from the root

View on GitHub (pinned to 81131a70a4)