dotnet/wpf · error · InvalidOperationException
SR.Format(SR.Invalid_IInputElement, element.GetType())
Error message
SR.Format(SR.Invalid_IInputElement, element.GetType())
What it means
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.
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
Example fix
// before
Keyboard.Focus(someDependencyObject);
// after
if (someDependencyObject is IInputElement inputElem)
Keyboard.Focus(inputElem); Defensive patterns
Strategy: type-guard
Validate before calling
bool focusable = element is IInputElement ie && InputElement.IsValid(ie);
Type guard
bool IsFocusable(object o) => o is IInputElement;
Try / catch
try { Keyboard.Focus(el); } catch (InvalidOperationException) { /* el is not an IInputElement */ } Prevention
- Only focus UIElement, ContentElement, or UIElement3D instances
- Remember DrawingVisual/raw Visual are not focusable input elements
When it happens
Trigger: 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.
Common situations: 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).
Understand the failure class
Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.
Related errors
- SR.Format(SR.Invalid_IInputElement, o.GetType())
- Animation_Invalid_DefaultValue
- Cannot remove signature from read-only file.
- Image_EncoderNoColorContext
- Image_EncoderNoGlobalMetadata
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/de989563bd00af0e.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/KeyboardDevice.cs:120
}
/// <summary>
/// Focuses the keyboard on a particular element.
/// </summary>
/// <param name="element">
/// The element to focus the keyboard on.
/// </param>
public IInputElement Focus(IInputElement element)
{
DependencyObject oFocus = null;
bool forceToNullIfFailed = false;
// Validate that elt is either a UIElement, a ContentElement or a UIElement3D.
if(element != null)
{
if(!InputElement.IsValid(element))
{
throw new InvalidOperationException(SR.Format(SR.Invalid_IInputElement, element.GetType()));
}
oFocus = (DependencyObject) element;
}
// If no element is given for focus, use the root of the active source.
if(oFocus == null && _activeSource != null)
{
oFocus = _activeSource.RootVisual;
forceToNullIfFailed = true;
}
Focus(oFocus, true, true, forceToNullIfFailed);
return (IInputElement) _focus;
}
private void Focus(DependencyObject focus, bool askOld, bool askNew, bool forceToNullIfFailed)
{View on GitHub (pinned to 81131a70a4)