{"record":{"id":"0d9a657678bb39a8","repo":"dotnet/wpf","slug":"sr-invalid-iinputelement","errorCode":null,"errorMessage":"SR.Invalid_IInputElement","messagePattern":"SR\\.Invalid_IInputElement","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/UIElement.cs","lineNumber":1901,"sourceCode":"                uiElement.AddHandler(routedEvent, handler);\n            }\n            else\n            {\n                ContentElement contentElement = d as ContentElement;\n                if (contentElement != null)\n                {\n                    contentElement.AddHandler(routedEvent, handler);\n                }\n                else\n                {\n                    UIElement3D uiElement3D = d as UIElement3D;\n                    if (uiElement3D != null)\n                    {\n                        uiElement3D.AddHandler(routedEvent, handler);\n                    }\n                    else\n                    {\n                        throw new ArgumentException(SR.Format(SR.Invalid_IInputElement, d.GetType()));\n                    }\n                }\n            }\n        }\n\n        /// <summary>\n        ///     Removes a handler for the given attached event\n        /// </summary>\n        internal static void RemoveHandler(DependencyObject d, RoutedEvent routedEvent, Delegate handler)\n        {\n            ArgumentNullException.ThrowIfNull(d);\n\n            Debug.Assert(routedEvent != null, \"RoutedEvent must not be null\");\n\n            UIElement uiElement = d as UIElement;\n            if (uiElement != null)\n            {\n                uiElement.RemoveHandler(routedEvent, handler);","sourceCodeStart":1883,"sourceCodeEnd":1919,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/UIElement.cs#L1883-L1919","documentation":"The static UIElement.AddHandler helper only accepts targets that are UIElement, UIElement3D, or ContentElement. When the given DependencyObject d is none of these, ArgumentException(SR.Invalid_IInputElement) is thrown with the actual type name.","triggerScenarios":"Calling UIElement.AddHandler(routedEvent, d, handler) with a DependencyObject that is not an event-capable element (e.g. a raw DependencyObject, Freezable, or other non-IInputElement).","commonSituations":"Generic helper code written against DependencyObject instead of UIElement/ContentElement; wiring handlers on non-element objects like brushes or animations; refactors that widened parameter types.","solutions":["Pass a UIElement, UIElement3D, or ContentElement as the target","Check the target type before calling and route to the correct AddHandler overload","Store handlers on a containing element if the object cannot own routed-event handlers"],"exampleFix":"// before\nUIElement.AddHandler(ev, (DependencyObject)brush, handler); // throws\n// after\nif (d is UIElement ue) ue.AddHandler(ev, handler);\nelse if (d is ContentElement ce) ce.AddHandler(ev, handler);\nelse throw new NotSupportedException($\"{d.GetType()} cannot own routed event handlers\");","handlingStrategy":"validation","validationCode":"if (!(d is UIElement || d is UIElement3D || d is ContentElement))\n    throw new ArgumentException($\"{d.GetType()} cannot own routed-event handlers\");","typeGuard":"static bool CanAddHandlers(DependencyObject d) => d is UIElement || d is UIElement3D || d is ContentElement;","tryCatchPattern":"try { UIElement.AddHandler(ev, d, handler); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"IInputElement\")) { log.Warn($\"Cannot attach handler to {d?.GetType()}\", ex); }","preventionTips":["Type event-target parameters as UIElement/ContentElement, not DependencyObject","Attach handlers only on elements, not on Freezables or plain DependencyObjects","Write unit tests for helper wiring code with a non-element DependencyObject"],"tags":["wpf","routed-events","type-mismatch","argument-validation"],"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"}