{"record":{"id":"bf7664c81fb11f7d","repo":"dotnet/wpf","slug":"sr-classtypeillegal","errorCode":null,"errorMessage":"SR.ClassTypeIllegal","messagePattern":"SR\\.ClassTypeIllegal","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/EventManager.cs","lineNumber":159,"sourceCode":"        /// </param>\n        /// <ExternalAPI/>\n        public static void RegisterClassHandler(\n            Type classType,\n            RoutedEvent routedEvent,\n            Delegate handler,\n            bool handledEventsToo)\n        {\n            ArgumentNullException.ThrowIfNull(classType);\n\n            ArgumentNullException.ThrowIfNull(routedEvent);\n\n            ArgumentNullException.ThrowIfNull(handler);\n\n            if (!typeof(UIElement).IsAssignableFrom(classType) &&\n                !typeof(ContentElement).IsAssignableFrom(classType) &&\n                !typeof(UIElement3D).IsAssignableFrom(classType))\n            {\n                throw new ArgumentException(SR.ClassTypeIllegal);\n            }\n            \n            if (!routedEvent.IsLegalHandler(handler))\n            {\n                throw new ArgumentException(SR.HandlerTypeIllegal);\n            }\n            \n            GlobalEventManager.RegisterClassHandler(classType, routedEvent, handler, handledEventsToo);\n        }\n\n        /// <summary>\n        ///     Returns <see cref=\"RoutedEvent\"/>s \n        ///     that have been registered so far\n        /// </summary>\n        /// <remarks>\n        ///     Also see \n        ///     <see cref=\"EventManager.RegisterRoutedEvent\"/>\n        ///     <para/>","sourceCodeStart":141,"sourceCodeEnd":177,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/EventManager.cs#L141-L177","documentation":"EventManager.RegisterClassHandler only accepts class types deriving from UIElement, ContentElement, or UIElement3D, since class handling is implemented on those element cores. A classType outside that hierarchy throws ArgumentException with SR.ClassTypeIllegal. This is validated before the handler-legality check.","triggerScenarios":"Calling RegisterClassHandler with a type that does not derive from UIElement/ContentElement/UIElement3D — e.g. a plain Control-derived non-visual class, a service class, or a DependencyObject that is not an element.","commonSituations":"Attempting class handling on DispatcherObject/DependencyObject subclasses; wiring handlers for custom non-element classes; typo passing typeof(HandlerClass) instead of typeof(ElementClass).","solutions":["Pass a class type deriving from UIElement, UIElement3D, or ContentElement","Register handlers on the nearest element base class that is in the allowed hierarchy","Move the handler registration to the element class itself instead of an unrelated class"],"exampleFix":"// before\nEventManager.RegisterClassHandler(typeof(MyService), Mouse.MouseDownEvent, handler, false);\n// after\nEventManager.RegisterClassHandler(typeof(MyElement), Mouse.MouseDownEvent, new MouseButtonEventHandler(handler), false);","handlingStrategy":"validation","validationCode":"bool ok = typeof(UIElement).IsAssignableFrom(classType) || typeof(ContentElement).IsAssignableFrom(classType) || typeof(UIElement3D).IsAssignableFrom(classType);","typeGuard":"bool IsClassHandlerTarget(Type t) => typeof(UIElement).IsAssignableFrom(t) || typeof(ContentElement).IsAssignableFrom(t) || typeof(UIElement3D).IsAssignableFrom(t);","tryCatchPattern":"try { EventManager.RegisterClassHandler(classType, evt, handler, false); } catch (ArgumentException) { /* classType not an element type */ }","preventionTips":["Only register class handlers on UIElement/UIElement3D/ContentElement-derived types","Constrain generic helpers with `where T : UIElement`","Double-check typeof() arguments for typos"],"tags":["wpf","routed-events","class-handler","argument"],"backgroundTag":"invalid-argument-value","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"}