{"record":{"id":"07a6ad10359c085d","repo":"dotnet/wpf","slug":"sr-invalid-iinputelement-newfocus-gettype","errorCode":null,"errorMessage":"SR.Invalid_IInputElement (newFocus.GetType())","messagePattern":"SR\\.Invalid_IInputElement \\(newFocus\\.GetType\\(\\)\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/FocusChangedEventArgs.cs","lineNumber":32,"sourceCode":"        /// <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.\n        /// </summary>\n        public IInputElement NewFocus\n        {","sourceCodeStart":14,"sourceCodeEnd":50,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/FocusChangedEventArgs.cs#L14-L50","documentation":"The KeyboardFocusChangedEventArgs constructor validates newFocus via InputElement.IsValid and throws InvalidOperationException(SR.Invalid_IInputElement, newFocus.GetType()) when newFocus is non-null but not a type WPF's keyboard system accepts (a UIElement-derived or ContentElement-derived element). Directly implementing IInputElement is not sufficient.","triggerScenarios":"new KeyboardFocusChangedEventArgs(keyboard, timestamp, oldFocus, newFocus) with a non-null newFocus that is a hand-rolled IInputElement implementation or an unrelated object cast to IInputElement.","commonSituations":"Simulating keyboard focus in automation/tests with fake objects; passing proxy/wrapper objects around real visuals; interop code supplying non-WPF elements.","solutions":["Ensure newFocus is an instance of UIElement or ContentElement (or derived class).","Pass null when there is no new focus target.","Call InputElement.IsValid(newFocus) before constructing the args and handle invalid values.","In tests, create real FrameworkElement instances instead of IInputElement mocks."],"exampleFix":"// before\nIInputElement newFocus = Substitute.For<IInputElement>();\nvar args = new KeyboardFocusChangedEventArgs(kb, ts, oldFocus, newFocus);\n// after\nIInputElement newFocus = new Border(); // real UIElement\nvar args = new KeyboardFocusChangedEventArgs(kb, ts, oldFocus, newFocus);","handlingStrategy":"type-guard","validationCode":"if (newFocus != null && newFocus is not (UIElement or ContentElement))\n    throw new InvalidOperationException($\"newFocus must be UIElement/ContentElement, got {newFocus.GetType()}\");","typeGuard":"static bool IsValidFocusTarget(IInputElement e) => e is null or UIElement or ContentElement;","tryCatchPattern":"try { args = new KeyboardFocusChangedEventArgs(kb, ts, oldFocus, newFocus); }\ncatch (InvalidOperationException ex)\n{ logger.LogError(\"Bad newFocus: {Msg}\", ex.Message); return; }","preventionTips":["Ensure focus targets are UIElement or ContentElement derived types.","Avoid passing wrapper/proxy objects as IInputElement to WPF input APIs.","Use real visual elements in automation instead of fake IInputElement implementations.","Narrow with pattern matching (is UIElement) before constructing event args."],"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"}