{"record":{"id":"52d79db93b0b2482","repo":"dotnet/wpf","slug":"sr-format-sr-nullpropertyillegal-eventsetter-handler","errorCode":null,"errorMessage":"SR.Format(SR.NullPropertyIllegal, \"EventSetter.Handler\")","messagePattern":"SR\\.Format\\(SR\\.NullPropertyIllegal, \"EventSetter\\.Handler\"\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/EventSetter.cs","lineNumber":99,"sourceCode":"            }\n        }\n\n\n        //\n        //  Do the error checking that we can only do once all of the properties have been\n        //  set, then call up to base.\n        //\n        \n        internal override void Seal()\n        {\n\n            if (_event == null)\n            {\n                throw new ArgumentException(SR.Format(SR.NullPropertyIllegal, \"EventSetter.Event\"));\n            }\n            if (_handler == null)\n            {\n                throw new ArgumentException(SR.Format(SR.NullPropertyIllegal, \"EventSetter.Handler\"));\n            }\n            if (_handler.GetType() != _event.HandlerType)\n            {\n                throw new ArgumentException(SR.HandlerTypeIllegal);\n            }\n\n            base.Seal();\n            \n        }\n\n\n        private RoutedEvent    _event;\n        private Delegate         _handler;\n        private bool             _handledEventsToo;\n    }\n    \n}\n","sourceCodeStart":81,"sourceCodeEnd":117,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/EventSetter.cs#L81-L117","documentation":"EventSetter.Seal throws ArgumentException naming EventSetter.Handler when the Handler property is null. An EventSetter exists solely to attach a delegate, so a missing handler makes the setter invalid once the owning style is sealed.","triggerScenarios":"Adding an EventSetter with only Event set (no Handler) to a Style, then the style is sealed when applied/used.","commonSituations":"XAML <EventSetter Event=\"Button.Click\"/> without a Handler attribute; programmatically built styles where the handler assignment was skipped or a bound handler resolved to null.","solutions":["Provide Handler in XAML: <EventSetter Event=\"Button.Click\" Handler=\"OnClick\"/>","In code, set setter.Handler = new RoutedEventHandler(OnClick) before the style is used","Verify code-behind method names referenced in XAML actually exist so the handler isn't left null"],"exampleFix":"// before\nvar es = new EventSetter { Event = ButtonBase.ClickEvent };\nstyle.Setters.Add(es); // throws on Seal\n// after\nvar es = new EventSetter { Event = ButtonBase.ClickEvent, Handler = new RoutedEventHandler(OnClick) };\nstyle.Setters.Add(es);","handlingStrategy":"validation","validationCode":"if (eventSetter.Handler == null) throw new InvalidOperationException(\"EventSetter.Handler must be set\");","typeGuard":"bool HasHandler(EventSetter s) => s?.Handler != null;","tryCatchPattern":"try { style.Seal(); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"EventSetter.Handler\")) { attachHandler(); }","preventionTips":["Ensure Handler is assigned before styles are applied","Confirm code-behind handler methods exist for all XAML references"],"tags":["wpf","style","eventsetter","xaml"],"backgroundTag":"missing-required-argument","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"}