{"record":{"id":"0de7ad0a5862e150","repo":"stride3d/stride","slug":"the-eventname-property-must-be-set-on-behavior-gettype","errorCode":null,"errorMessage":"The EventName property must be set on behavior '{GetType().FullName}'.","messagePattern":"The EventName property must be set on behavior '(.+?)'\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"sources/presentation/Stride.Core.Presentation.Wpf/Behaviors/OnEventBehavior.cs","lineNumber":67,"sourceCode":"        /// Gets or sets the type that owns the event when <see cref=\"EventName\"/> describes a <see cref=\"RoutedEvent\"/>.\n        /// </summary>\n        public Type EventOwnerType { get { return (Type)GetValue(EventOwnerTypeProperty); } set { SetValue(EventOwnerTypeProperty, value); } }\n\n        /// <summary>\n        /// Gets or sets whether to set the event as handled.\n        /// </summary>\n        public bool HandleEvent { get { return (bool)GetValue(HandleEventProperty); } set { SetValue(HandleEventProperty, value.Box()); } }\n\n        /// <summary>\n        /// Invoked when the monitored event is raised.\n        /// </summary>\n        protected abstract void OnEvent();\n\n        /// <inheritdoc/>\n        protected override void OnAttached()\n        {\n            if (EventName == null)\n                throw new ArgumentException($\"The EventName property must be set on behavior '{GetType().FullName}'.\");\n\n            var eventOwnerType = EventOwnerType ?? AssociatedObject.GetType();\n\n            var uiElement = AssociatedObject as UIElement;\n\n            var routedEvents = EventManager.GetRoutedEvents().Where(x => x.Name == EventName && x.OwnerType.IsAssignableFrom(eventOwnerType)).ToArray();\n\n            if (uiElement != null && routedEvents.Length > 0)\n            {\n                if (routedEvents.Length > 1)\n                    throw new NotImplementedException(\"TODO: several events found, find a way to decide the most relevant one.\");\n\n                routedEvent = routedEvents.First();\n                uiElement.AddHandler(routedEvent, routedEventHandler);\n            }\n            else\n            {\n                var eventInfo = AssociatedObject.GetType().GetEvent(EventName);","sourceCodeStart":49,"sourceCodeEnd":85,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/presentation/Stride.Core.Presentation.Wpf/Behaviors/OnEventBehavior.cs#L49-L85","documentation":"OnEventBehavior.OnAttached throws ArgumentException when the EventName attached property was not set before the behavior attaches to its associated object. The behavior needs an event name to resolve and subscribe to. The message includes the behavior's concrete type to help locate the misconfigured XAML.","triggerScenarios":"Using a behavior derived from OnEventBehavior in XAML or code without setting the EventName property, e.g. <b:OnKeyUpBehavior/> with no EventName attribute.","commonSituations":"Copy-pasted XAML where the EventName attribute was dropped; renamed properties leaving EventName unset; creating the behavior programmatically without assigning EventName before attachment.","solutions":["Set EventName on the behavior in XAML, e.g. EventName=\"MouseLeftButtonDown\".","If setting in code, assign EventName before the behavior is attached to the element.","Check the behavior type named in the message to find the offending XAML element."],"exampleFix":"// before\n<behaviors:OnEventBehavior /> <!-- no EventName -->\n// after\n<behaviors:OnEventBehavior EventName=\"Click\" />","handlingStrategy":"validation","validationCode":"if (string.IsNullOrEmpty(OnEventBehavior.GetEventName(myBehavior)))\n    throw new InvalidOperationException(\"EventName must be set before the behavior attaches.\");","typeGuard":"static bool HasEventName(OnEventBehavior b) => !string.IsNullOrEmpty(b.EventName);","tryCatchPattern":"try\n{\n    element.Behaviors.Add(behavior);\n}\ncatch (ArgumentException ex) when (ex.Message.Contains(\"EventName\"))\n{\n    logger.Error(ex, \"Behavior attached without EventName\");\n}","preventionTips":["Always set EventName in XAML when declaring OnEventBehavior derivatives.","When constructing in code, set EventName before adding to Behaviors collection.","Add a design-time validation or default value for EventName in custom behaviors."],"tags":["wpf","xaml","behavior","missing-property"],"backgroundTag":"missing-required-argument","analyzedSha":"96fad776d210c221682aac1ccdf4c79dc046fc38","analyzedAt":"2026-09-14T02:59:31.279Z","contentChangedAt":"2026-09-14T02:59:31.279Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}