{"record":{"id":"fd58efceef00a665","repo":"dotnet/wpf","slug":"sr-invalid-iinputelement-oldfocus-gettype","errorCode":null,"errorMessage":"SR.Invalid_IInputElement (oldFocus.GetType())","messagePattern":"SR\\.Invalid_IInputElement \\(oldFocus\\.GetType\\(\\)\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/FocusChangedEventArgs.cs","lineNumber":29,"sourceCode":"        /// <summary>\n        ///     Constructs an instance of the KeyboardFocusChangedEventArgs class.\n        /// </summary>\n        /// <param name=\"keyboard\">\n        ///     The logical keyboard device associated with this event.\n        /// </param>\n        /// <param name=\"timestamp\">\n        ///     The time when the input occurred.\n        /// </param>\n        /// <param name=\"oldFocus\">\n        ///     The element that previously had focus.\n        /// </param>\n        /// <param name=\"newFocus\">\n        ///     The element that now has focus.\n        /// </param>\n        public KeyboardFocusChangedEventArgs(KeyboardDevice keyboard, int timestamp, IInputElement oldFocus, IInputElement newFocus) : base(keyboard, timestamp)\n        {\n            if (oldFocus != null && !InputElement.IsValid(oldFocus))\n                throw new InvalidOperationException(SR.Format(SR.Invalid_IInputElement, oldFocus.GetType()));\n\n            if (newFocus != null && !InputElement.IsValid(newFocus))\n                throw new InvalidOperationException(SR.Format(SR.Invalid_IInputElement, newFocus.GetType()));\n\n            _oldFocus = oldFocus;\n            _newFocus = newFocus;\n        }\n\n        /// <summary>\n        ///     The element that previously had focus.\n        /// </summary>\n        public IInputElement OldFocus\n        {\n            get {return _oldFocus;}\n        }\n\n        /// <summary>\n        ///     The element that now has focus.","sourceCodeStart":11,"sourceCodeEnd":47,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/FocusChangedEventArgs.cs#L11-L47","documentation":"The KeyboardFocusChangedEventArgs constructor validates oldFocus via InputElement.IsValid and throws InvalidOperationException(SR.Invalid_IInputElement, oldFocus.GetType()) if it is non-null but is not a valid IInputElement — i.e. it is neither a ContentElement, nor a UIElement (or its valid subclasses) that WPF's input system can target. The keyboard input system can only raise focus events against these element types.","triggerScenarios":"new KeyboardFocusChangedEventArgs(keyboard, timestamp, oldFocus, newFocus) with a non-null oldFocus that is a custom object implementing IInputElement directly instead of deriving from UIElement/ContentElement.","commonSituations":"Custom framework classes implementing IInputElement manually; passing mock objects in unit tests; passing elements from a different presentation source or a non-WPF object that merely implements the interface.","solutions":["Derive the focus element from UIElement (or ContentElement for document content) instead of implementing IInputElement directly.","Pass null if there is no previous focus element rather than a placeholder object.","Use InputElement.IsValid(element) in a guard before constructing the event args.","Replace test mocks with real UIElement instances or derived test doubles."],"exampleFix":"// before\nclass MyFocusTarget : IInputElement { ... }\nvar args = new KeyboardFocusChangedEventArgs(kb, ts, new MyFocusTarget(), newFocus);\n// after\nclass MyFocusTarget : UIElement { ... }\nvar args = new KeyboardFocusChangedEventArgs(kb, ts, new MyFocusTarget(), newFocus);","handlingStrategy":"type-guard","validationCode":"if (oldFocus != null && !InputElement.IsValid(oldFocus))\n    throw new InvalidOperationException($\"Invalid focus element: {oldFocus.GetType()}\");","typeGuard":"static bool IsValidFocusElement(IInputElement e) => e is null || InputElement.IsValid(e);\n// or without internals: e is UIElement or e is ContentElement","tryCatchPattern":"try { args = new KeyboardFocusChangedEventArgs(kb, ts, oldFocus, newFocus); }\ncatch (InvalidOperationException ex)\n{ logger.LogError(\"Invalid focus element: {Msg}\", ex.Message); args = null; }","preventionTips":["Derive focus targets from UIElement/ContentElement, never hand-implement IInputElement.","Pass null instead of placeholder objects for 'no previous focus'.","In tests, use real FrameworkElement instances rather than IInputElement mocks.","Validate elements with InputElement.IsValid (or a UIElement/ContentElement pattern check) before use."],"tags":["wpf","focus","input","invalid-argument"],"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"}