{"record":{"id":"de989563bd00af0e","repo":"dotnet/wpf","slug":"sr-format-sr-invalid-iinputelement-element-gettype","errorCode":null,"errorMessage":"SR.Format(SR.Invalid_IInputElement, element.GetType())","messagePattern":"SR\\.Format\\(SR\\.Invalid_IInputElement, element\\.GetType\\(\\)\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/KeyboardDevice.cs","lineNumber":120,"sourceCode":"        }\n\n        /// <summary>\n        ///     Focuses the keyboard on a particular element.\n        /// </summary>\n        /// <param name=\"element\">\n        ///     The element to focus the keyboard on.\n        /// </param>\n        public IInputElement Focus(IInputElement element)\n        {\n            DependencyObject oFocus = null;\n            bool forceToNullIfFailed = false;\n\n            // Validate that elt is either a UIElement, a ContentElement or a UIElement3D.\n            if(element != null)\n            {\n                if(!InputElement.IsValid(element))\n                {\n                    throw new InvalidOperationException(SR.Format(SR.Invalid_IInputElement, element.GetType()));\n                }\n\n                oFocus = (DependencyObject) element;\n            }\n\n            // If no element is given for focus, use the root of the active source.\n            if(oFocus == null && _activeSource != null)\n            {\n                oFocus = _activeSource.RootVisual;\n                forceToNullIfFailed = true;\n            }\n\n            Focus(oFocus, true, true, forceToNullIfFailed);\n\n            return (IInputElement) _focus;\n        }\n        private void Focus(DependencyObject focus, bool askOld, bool askNew, bool forceToNullIfFailed)\n        {","sourceCodeStart":102,"sourceCodeEnd":138,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/KeyboardDevice.cs#L102-L138","documentation":"KeyboardDevice.Focus validates that the element being focused is a valid IInputElement (a UIElement, ContentElement, or UIElement3D per InputElement.IsValid). If a non-input element is passed, it throws InvalidOperationException with Invalid_IInputElement.","triggerScenarios":"Calling Keyboard.Focus(element) or keyboardDevice.Focus(element) with an object that derives from DependencyObject but does not implement IInputElement properly, or a raw non-element object.","commonSituations":"Focusing a non-visual DependencyObject (e.g. a free-standing DependencyObject or Binding proxy); confusion between Visual and logical elements in templates; passing a DrawingVisual (not an IInputElement).","solutions":["Call Focus only with UIElement, ContentElement, or UIElement3D instances","Check InputElement.IsValid(element) or 'element is IInputElement' before focusing","Find the nearest focusable ancestor (via UIElement/ContentElement) and focus that instead"],"exampleFix":"// before\nKeyboard.Focus(someDependencyObject);\n// after\nif (someDependencyObject is IInputElement inputElem)\n    Keyboard.Focus(inputElem);","handlingStrategy":"type-guard","validationCode":"bool focusable = element is IInputElement ie && InputElement.IsValid(ie);","typeGuard":"bool IsFocusable(object o) => o is IInputElement;","tryCatchPattern":"try { Keyboard.Focus(el); } catch (InvalidOperationException) { /* el is not an IInputElement */ }","preventionTips":["Only focus UIElement, ContentElement, or UIElement3D instances","Remember DrawingVisual/raw Visual are not focusable input elements"],"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-22T01:17:13.364Z"}