dotnet/wpf · error · InvalidOperationException

SR.Format(SR.Invalid_IInputElement, o.GetType())

Error message

SR.Format(SR.Invalid_IInputElement, o.GetType())

What it means

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.

Solutions

  1. Ensure only UIElement, ContentElement, or UIElement3D objects ever receive keyboard focus
  2. Re-audit custom code that writes to FocusManager.FocusedElement or keyboard focus directly
  3. Clear focus with Keyboard.ClearFocus() / FocusManager pattern instead of assigning unsupported objects

Example fix

// before
focusScope.SetValue(FocusManager.FocusedElementProperty, arbitraryObject);
// after
if (arbitraryObject is UIElement uie)
    focusScope.SetValue(FocusManager.FocusedElementProperty, uie);
Defensive patterns

Strategy: validation

Validate before calling

bool supported = o is UIElement || o is ContentElement || o is UIElement3D;

Type guard

bool IsInputElement(DependencyObject o) => o is UIElement || o is ContentElement || o is UIElement3D;

Try / catch

try { Keyboard.Focus(el); } catch (InvalidOperationException) { /* old focus target was invalid */ }

Prevention

When it happens

Trigger: 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.

Common situations: Custom focus management assigning FocusManager/K FocusScope focus to unsupported elements; interop or automation tools poking focus to arbitrary objects.

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


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/14d8af888974e964. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/KeyboardDevice.cs:406

                            uie.IsEnabledChanged -= _isEnabledChangedEventHandler;
                            uie.IsVisibleChanged -= _isVisibleChangedEventHandler;
                            uie.FocusableChanged -= _focusableChangedEventHandler;
                        }
                        else if (o is ContentElement ce)
                        {
                            ce.IsEnabledChanged -= _isEnabledChangedEventHandler;
                            // NOTE: there is no IsVisible property for ContentElements.
                            ce.FocusableChanged -= _focusableChangedEventHandler;
                        }
                        else if (o is UIElement3D uie3D)
                        {
                            uie3D.IsEnabledChanged -= _isEnabledChangedEventHandler;
                            uie3D.IsVisibleChanged -= _isVisibleChangedEventHandler;
                            uie3D.FocusableChanged -= _focusableChangedEventHandler;
                        }
                        else
                        {
                            throw new InvalidOperationException(SR.Format(SR.Invalid_IInputElement, o.GetType())); 
                        }
                    }
                    if(_focus != null)
                    {
                        o = _focus;
                        if (o is UIElement uie)
                        {
                            uie.IsEnabledChanged += _isEnabledChangedEventHandler;
                            uie.IsVisibleChanged += _isVisibleChangedEventHandler;
                            uie.FocusableChanged += _focusableChangedEventHandler;
                        }                        
                        else if (o is ContentElement ce)
                        {
                            ce.IsEnabledChanged += _isEnabledChangedEventHandler;
                            // NOTE: there is no IsVisible property for ContentElements.
                            ce.FocusableChanged += _focusableChangedEventHandler;
                        }
                        else if (o is UIElement3D uie3D)

View on GitHub (pinned to 81131a70a4)