dotnet/wpf · error · ArgumentException

Event not found: Event 'eventName' not found on type…

Error message

Event not found: Event 'eventName' not found on type 'TEventSource'.

What it means

The private WeakEventManager<TEventSource,TEventArgs> constructor resolves the event by name via Type.GetEvent on TEventSource. If no public event with that name exists, _eventInfo is null and it throws ArgumentException with SR.EventNotFound ('Event not found: Event {type} not found on type {source}.'). This is a fail-fast guard so the reflection-based manager is never built against a nonexistent event.

Solutions

  1. Verify the exact spelling and case of the event name against typeof(TEventSource) — use nameof(SourceType.EventName) instead of a literal string.
  2. Confirm the event is public and declared on (or inherited by) TEventSource; GetEvent does not return non-public events.
  3. If the event was renamed, update all AddHandler/RemoveHandler call sites to the new name.
  4. Check with typeof(TEventSource).GetEvent("YourEvent") in the debugger/immediate window to confirm the reflection lookup succeeds.

Example fix

// before
WeakEventManager<MyControl, RoutedEventArgs>.AddHandler(control, "Clic", OnClick);
// after
WeakEventManager<MyControl, RoutedEventArgs>.AddHandler(control, nameof(MyControl.Click), OnClick);
Defensive patterns

Strategy: validation

Validate before calling

var eventInfo = typeof(TEventSource).GetEvent(eventName);
if (eventInfo == null)
    throw new InvalidOperationException($"Event '{eventName}' does not exist on {typeof(TEventSource).FullName}. Available: {string.Join(", ", typeof(TEventSource).GetEvents().Select(e => e.Name))}");

WeakEventManager<TEventSource, TEventArgs>.AddHandler(source, eventName, handler);

Type guard

static bool EventExists<TSrc>(string eventName) => typeof(TSrc).GetEvent(eventName) != null;

Try / catch

try
{
    WeakEventManager<TEventSource, TEventArgs>.AddHandler(source, eventName, handler);
}
catch (ArgumentException ex) when (ex.Message.Contains("Event not found"))
{
    logger.LogError(ex, "Event '{Event}' not found on {Type}", eventName, typeof(TEventSource).Name);
    throw;
}

Prevention

When it happens

Trigger: Calling WeakEventManager<TEventSource,TEventArgs>.AddHandler(source, "Click", handler) (or RemoveHandler / the constructor) where the string eventName does not match any public event on TEventSource — typo, case mismatch, event renamed, or event defined on a derived/interface type not exposed by TEventSource.

Common situations: Renaming an event during refactoring while string-based handler registrations still use the old name; using the CLR event name when the class exposes it under a different registered name; targeting events declared private/internal (GetEvent only finds public events).

Understand the failure class

Background: 'Could not be found', 'does not exist', 'not found in database': the resource-not-found family when an ID, slug, key, or URI lookup comes back empty — this error's family across 20 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/WeakEventManagerT.cs:28

namespace System.Windows
{
    public class WeakEventManager<TEventSource, TEventArgs> : WeakEventManager
        where TEventArgs : EventArgs
    {
        #region Constructors

        //
        //  Constructors
        //

        private WeakEventManager(string eventName)
        {
            _eventName = eventName;
            _eventInfo = typeof(TEventSource).GetEvent(_eventName);

            if (_eventInfo == null)
                throw new ArgumentException(SR.Format(SR.EventNotFound, typeof(TEventSource).FullName, eventName));

            _handler = Delegate.CreateDelegate(_eventInfo.EventHandlerType, this, DeliverEventMethodInfo);
        }

        #endregion Constructors

        #region Public Methods

        //
        //  Public Methods
        //

        /// <summary>
        /// Add a handler for the given source's event.
        /// </summary>
        public static void AddHandler(TEventSource source, string eventName, EventHandler<TEventArgs> handler)
        {
            ArgumentNullException.ThrowIfNull(handler);

View on GitHub (pinned to 81131a70a4)