{"record":{"id":"9362b49ec5bc4133","repo":"dotnet/wpf","slug":"sr-format-sr-invalid-iinputelement-dotarget-gettype","errorCode":null,"errorMessage":"SR.Format(SR.Invalid_IInputElement, doTarget.GetType())","messagePattern":"SR\\.Format\\(SR\\.Invalid_IInputElement, doTarget\\.GetType\\(\\)\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/PresentationSource.cs","lineNumber":699,"sourceCode":"                SourceChangedEventArgs args = new SourceChangedEventArgs(cachedSource, realSource)\n                {\n                    RoutedEvent = SourceChangedEvent\n                };\n                if (doTarget is UIElement uiElement)\n                {\n                    uiElement.RaiseEvent(args);\n                }                \n                else if (doTarget is ContentElement contentElement)\n                {\n                    contentElement.RaiseEvent(args);\n                }\n                else if (doTarget is UIElement3D uiElement3D)\n                {\n                    uiElement3D.RaiseEvent(args);\n                }\n                else\n                {\n                    throw new InvalidOperationException(SR.Format(SR.Invalid_IInputElement, doTarget.GetType())); \n                }\n\n                calledOut = true;\n            }\n\n            return calledOut;\n        }\n\n        #endregion\n\n        //------------------------------------------------------\n        //\n        //  Private Static Members\n        //\n        //------------------------------------------------------  \n\n        #region Private Static Members\n        // We use a private DP for the RootSource (the connection from the root","sourceCodeStart":681,"sourceCodeEnd":717,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/PresentationSource.cs#L681-L717","documentation":"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.","triggerScenarios":"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.","commonSituations":"Custom visual trees containing foreign DependencyObjects presented as element roots; interop hosts swapping in non-WPF roots; assembly version mismatches during upgrades.","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."],"exampleFix":"// before\npresentationSource.RootVisual = (Visual)foreignObject; // not a UIElement\n\n// after\npresentationSource.RootVisual = new RootVisualHost(); // derives from UIElement","handlingStrategy":"type-guard","validationCode":"if (!(doTarget is UIElement || doTarget is ContentElement || doTarget is UIElement3D))\n    throw new InvalidOperationException(\"PresentationSource root must be a recognized input element\");","typeGuard":"static bool CanRaiseSourceChanged(DependencyObject d) =>\n    d is UIElement || d is ContentElement || d is UIElement3D;","tryCatchPattern":null,"preventionTips":["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."],"tags":["wpf","internal-invariant","visual-tree"],"backgroundTag":"internal-invariant-violation","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}