{"record":{"id":"14d8af888974e964","repo":"dotnet/wpf","slug":"sr-format-sr-invalid-iinputelement-o-gettype","errorCode":null,"errorMessage":"SR.Format(SR.Invalid_IInputElement, o.GetType())","messagePattern":"SR\\.Format\\(SR\\.Invalid_IInputElement, o\\.GetType\\(\\)\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/KeyboardDevice.cs","lineNumber":406,"sourceCode":"                            uie.IsEnabledChanged -= _isEnabledChangedEventHandler;\n                            uie.IsVisibleChanged -= _isVisibleChangedEventHandler;\n                            uie.FocusableChanged -= _focusableChangedEventHandler;\n                        }\n                        else if (o is ContentElement ce)\n                        {\n                            ce.IsEnabledChanged -= _isEnabledChangedEventHandler;\n                            // NOTE: there is no IsVisible property for ContentElements.\n                            ce.FocusableChanged -= _focusableChangedEventHandler;\n                        }\n                        else if (o is UIElement3D uie3D)\n                        {\n                            uie3D.IsEnabledChanged -= _isEnabledChangedEventHandler;\n                            uie3D.IsVisibleChanged -= _isVisibleChangedEventHandler;\n                            uie3D.FocusableChanged -= _focusableChangedEventHandler;\n                        }\n                        else\n                        {\n                            throw new InvalidOperationException(SR.Format(SR.Invalid_IInputElement, o.GetType())); \n                        }\n                    }\n                    if(_focus != null)\n                    {\n                        o = _focus;\n                        if (o is UIElement uie)\n                        {\n                            uie.IsEnabledChanged += _isEnabledChangedEventHandler;\n                            uie.IsVisibleChanged += _isVisibleChangedEventHandler;\n                            uie.FocusableChanged += _focusableChangedEventHandler;\n                        }                        \n                        else if (o is ContentElement ce)\n                        {\n                            ce.IsEnabledChanged += _isEnabledChangedEventHandler;\n                            // NOTE: there is no IsVisible property for ContentElements.\n                            ce.FocusableChanged += _focusableChangedEventHandler;\n                        }\n                        else if (o is UIElement3D uie3D)","sourceCodeStart":388,"sourceCodeEnd":424,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/KeyboardDevice.cs#L388-L424","documentation":"During ChangeFocus, when releasing focus from the previously focused element, the code checks whether the old element is UIElement, ContentElement, or UIElement3D; if it matches none of those it throws InvalidOperationException (Invalid_IInputElement), since the focus chain invariant requires every focused object to be one of these three types.","triggerScenarios":"Changing keyboard focus while the currently focused object (_focus) is a DependencyObject that is not UIElement/ContentElement/UIElement3D — usually set by custom code that bypassed validation.","commonSituations":"Custom focus management assigning FocusManager/K FocusScope focus to unsupported elements; interop or automation tools poking focus to arbitrary objects.","solutions":["Ensure only UIElement, ContentElement, or UIElement3D objects ever receive keyboard focus","Re-audit custom code that writes to FocusManager.FocusedElement or keyboard focus directly","Clear focus with Keyboard.ClearFocus() / FocusManager pattern instead of assigning unsupported objects"],"exampleFix":"// before\nfocusScope.SetValue(FocusManager.FocusedElementProperty, arbitraryObject);\n// after\nif (arbitraryObject is UIElement uie)\n    focusScope.SetValue(FocusManager.FocusedElementProperty, uie);","handlingStrategy":"validation","validationCode":"bool supported = o is UIElement || o is ContentElement || o is UIElement3D;","typeGuard":"bool IsInputElement(DependencyObject o) => o is UIElement || o is ContentElement || o is UIElement3D;","tryCatchPattern":"try { Keyboard.Focus(el); } catch (InvalidOperationException) { /* old focus target was invalid */ }","preventionTips":["Never assign non-element DependencyObjects to focus/FocusManager state","Use Keyboard.ClearFocus() instead of manual focus bookkeeping"],"tags":["wpf","focus","invalid-operation"],"backgroundTag":"type-mismatch","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"}