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
- Ensure the visual/root elements of a PresentationSource are UIElement/ContentElement/UIElement3D derived.
- Fix host/interop code that inserts non-element DependencyObjects into the WPF tree.
- Unify WPF assembly versions to avoid type identity mismatches.
- 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
- Set RootVisual only to UIElement-derived visuals.
- Do not insert foreign DependencyObjects into the WPF visual tree.
- Test interop root-swapping scenarios against the target WPF version.
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
- SR.Visual_ArgumentOutOfRange
- ' ' is not a Visual or Visual3D.
- inverse ? SR.Visual_NotADescendant : SR.Visual_NotAnAncestor
- Page can have only Window or Frame as parent.
- Specified index is out of range or child at index is null…
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 rootView on GitHub (pinned to 81131a70a4)