stride3d/stride · error · InvalidOperationException

Impossible to find a valid event named

Error message

Impossible to find a valid event named '{EventName}'.

What it means

OnEventBehavior.OnAttached throws InvalidOperationException when the event name could not be resolved as a routed event and the associated object's type exposes no CLR event with EventName. It means the behavior found no event at all to subscribe to. The message names the EventName that was searched.

Solutions

  1. Correct the EventName spelling to match an event that exists on the associated object.
  2. Verify the event exists on the attached control's type via its documentation/reflection.
  3. Set EventOwnerType if the event is an attached/routed event owned by another type.
  4. Use x:Bind of snoop/inspect the live visual tree to confirm available event names.

Example fix

// before
<behaviors:OnEventBehavior EventName="MouseLeftButonDown" /> <!-- typo -->
// after
<behaviors:OnEventBehavior EventName="MouseLeftButtonDown" />
Defensive patterns

Strategy: validation

Validate before calling

if (target.GetType().GetEvent(eventName) == null)
    throw new InvalidOperationException($"'{eventName}' is not an event on {target.GetType()}");

Type guard

static bool HasEvent(object target, string eventName) =>
    !string.IsNullOrEmpty(eventName) && target?.GetType().GetEvent(eventName) != null;

Try / catch

try
{
    element.Behaviors.Add(behavior);
}
catch (InvalidOperationException ex) when (ex.Message.Contains("Impossible to find a valid event"))
{
    logger.Error(ex, $"Event '{behavior.EventName}' does not exist on {element.GetType()}");
}

Prevention

When it happens

Trigger: Setting EventName to a name that matches no routed event on the UIElement and no CLR event on AssociatedObject.GetType() — typically a typo or an event that does not exist on that control type.

Common situations: Typo in EventName (e.g. "MouseLeftButonDown"); EventName valid on a different control class than the one the behavior is attached to; control API changed across framework versions.

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 stride3d/stride@96fad776d2 (2026-09-14). Data as JSON: /api/errors/fbd05e04304c4da6. Report an issue: GitHub.

Appendix: source

Thrown at sources/presentation/Stride.Core.Presentation.Wpf/Behaviors/OnEventBehavior.cs:88

            var uiElement = AssociatedObject as UIElement;

            var routedEvents = EventManager.GetRoutedEvents().Where(x => x.Name == EventName && x.OwnerType.IsAssignableFrom(eventOwnerType)).ToArray();

            if (uiElement != null && routedEvents.Length > 0)
            {
                if (routedEvents.Length > 1)
                    throw new NotImplementedException("TODO: several events found, find a way to decide the most relevant one.");

                routedEvent = routedEvents.First();
                uiElement.AddHandler(routedEvent, routedEventHandler);
            }
            else
            {
                var eventInfo = AssociatedObject.GetType().GetEvent(EventName);

                if (eventInfo == null)
                    throw new InvalidOperationException($"Impossible to find a valid event named '{EventName}'.");

                eventHandler = AnonymousEventHandler.RegisterEventHandler(eventInfo, AssociatedObject, OnEvent);
            }
        }

        /// <inheritdoc/>
        protected override void OnDetaching()
        {
            if (routedEvent != null)
            {
                var uiElement = (UIElement)AssociatedObject;
                uiElement.RemoveHandler(routedEvent, routedEventHandler);
                routedEvent = null;
            }
            else if (eventHandler != null)
            {
                AnonymousEventHandler.UnregisterEventHandler(eventHandler);
                eventHandler = null;

View on GitHub (pinned to 96fad776d2)