dotnet/wpf · error · ArgumentException

SR.Format(SR.NullPropertyIllegal, "EventSetter.Handler")

Error message

SR.Format(SR.NullPropertyIllegal, "EventSetter.Handler")

What it means

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.

Solutions

  1. Provide Handler in XAML: <EventSetter Event="Button.Click" Handler="OnClick"/>
  2. In code, set setter.Handler = new RoutedEventHandler(OnClick) before the style is used
  3. Verify code-behind method names referenced in XAML actually exist so the handler isn't left null

Example fix

// before
var es = new EventSetter { Event = ButtonBase.ClickEvent };
style.Setters.Add(es); // throws on Seal
// after
var es = new EventSetter { Event = ButtonBase.ClickEvent, Handler = new RoutedEventHandler(OnClick) };
style.Setters.Add(es);
Defensive patterns

Strategy: validation

Validate before calling

if (eventSetter.Handler == null) throw new InvalidOperationException("EventSetter.Handler must be set");

Type guard

bool HasHandler(EventSetter s) => s?.Handler != null;

Try / catch

try { style.Seal(); }
catch (ArgumentException ex) when (ex.Message.Contains("EventSetter.Handler")) { attachHandler(); }

Prevention

When it happens

Trigger: Adding an EventSetter with only Event set (no Handler) to a Style, then the style is sealed when applied/used.

Common situations: 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.

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/52d79db93b0b2482. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/EventSetter.cs:99

            }
        }


        //
        //  Do the error checking that we can only do once all of the properties have been
        //  set, then call up to base.
        //
        
        internal override void Seal()
        {

            if (_event == null)
            {
                throw new ArgumentException(SR.Format(SR.NullPropertyIllegal, "EventSetter.Event"));
            }
            if (_handler == null)
            {
                throw new ArgumentException(SR.Format(SR.NullPropertyIllegal, "EventSetter.Handler"));
            }
            if (_handler.GetType() != _event.HandlerType)
            {
                throw new ArgumentException(SR.HandlerTypeIllegal);
            }

            base.Seal();
            
        }


        private RoutedEvent    _event;
        private Delegate         _handler;
        private bool             _handledEventsToo;
    }
    
}

View on GitHub (pinned to 81131a70a4)