{"record":{"id":"07a752d2bff63df0","repo":"stride3d/stride","slug":"behavior-of-type-gettype-must-be-bound-to-objects-of-type","errorCode":null,"errorMessage":"Behavior of type '{GetType()}' must be bound to objects of type '{typeof(FrameworkElement)}'. (currently bound to object typed '{AssociatedObject.GetType()}')","messagePattern":"Behavior of type '(.+?)' must be bound to objects of type '(.+?)'\\. \\(currently bound to object typed '(.+?)'\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"sources/presentation/Stride.Core.Presentation.Wpf/Behaviors/OnFocusBindingInterruptionBehavior.cs","lineNumber":54,"sourceCode":"                throw new ArgumentException(\"PropertyName must be set.\");\n\n            if (Binding == null)\n            {\n                throw new ArgumentException($\"Binding must be set for {PropertyName} property of host '{AssociatedObject}' on behavior '{GetType().FullName}'.\");\n            }\n\n            property = AssociatedObject.GetDependencyProperties(true).FirstOrDefault(dp => dp.Name == PropertyName);\n\n            if (property == null /* need to check DesignMode as well ? */)\n                throw new InvalidOperationException($\"Impossible to find property named '{PropertyName}' on object typed '{AssociatedObject.GetType()}'.\");\n\n            if ((Binding is Binding) == false)\n                throw new InvalidOperationException(\"Not supported binding type.\");\n\n            var element = AssociatedObject as FrameworkElement;\n            if (element == null)\n            {\n                throw new InvalidOperationException(\n                    $\"Behavior of type '{GetType()}' must be bound to objects of type '{typeof(FrameworkElement)}'. (currently bound to object typed '{AssociatedObject.GetType()}')\");\n            }\n\n            BindingOperations.SetBinding(AssociatedObject, property, Binding);\n\n            // subscribe to *Focus events\n            element.GotFocus += OnHostGotFocus;\n            element.LostFocus += OnHostLostFocus;\n\n            subscriber = new AnonymousDisposable(() =>\n            {\n                // unsubscribe from *Focus events\n                element.LostFocus -= OnHostLostFocus;\n                element.GotFocus -= OnHostGotFocus;\n            });\n        }\n\n        protected override void OnDetaching()","sourceCodeStart":36,"sourceCodeEnd":72,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/presentation/Stride.Core.Presentation.Wpf/Behaviors/OnFocusBindingInterruptionBehavior.cs#L36-L72","documentation":"The behavior subscribes to focus events and manipulates bindings via BindingOperations, which requires the associated object to be a FrameworkElement. If AssociatedObject cannot be cast to FrameworkElement, OnAttached throws InvalidOperationException explaining the required type and the actual type of the bound object.","triggerScenarios":"Attaching OnFocusBindingInterruptionBehavior to a non-FrameworkElement such as a Freezable, BindingExpression target like a BindingMarker, a DependencyObject in a template that isn't an element, or collection items that are plain DependencyObjects.","commonSituations":"Dropping the behavior onto the wrong node in a template (e.g. on a GradientStop or a non-visual DependencyObject); using i:Interaction on objects where only triggers are legal; wiring the behavior in code to a generic DependencyObject.","solutions":["Move the behavior to a FrameworkElement (e.g. the TextBox/Control itself) inside Interaction.Behaviors.","If the target is inside a template, attach the behavior to the templated root FrameworkElement, not to a non-element node.","Adjust code-behind attachment so the behavior is attached to a Control/FrameworkElement instance."],"exampleFix":"<!-- before: behavior on a non-FrameworkElement node -->\n<GradientStop>\n  <i:Interaction.Behaviors>\n    <b:OnFocusBindingInterruptionBehavior PropertyName=\"Color\" Binding=\"{Binding Accent}\" />\n  </i:Interaction.Behaviors>\n</GradientStop>\n\n<!-- after: attach to the hosting control -->\n<TextBox Text=\"{Binding Accent}\">\n  <i:Interaction.Behaviors>\n    <b:OnFocusBindingInterruptionBehavior PropertyName=\"Text\" Binding=\"{Binding Accent}\" />\n  </i:Interaction.Behaviors>\n</TextBox>","handlingStrategy":"type-guard","validationCode":"if (!(associatedObject is FrameworkElement))\n    throw new InvalidOperationException($\"Behavior requires a FrameworkElement; got {associatedObject?.GetType().Name}.\");","typeGuard":"static bool CanHostFocusBehavior(DependencyObject o) => o is FrameworkElement;","tryCatchPattern":"try { AttachBehavior(behavior, host); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"must be bound to objects of type\")) { log.Error($\"Move behavior to a FrameworkElement, not {host.GetType().Name}\"); }","preventionTips":["Attach the behavior only inside FrameworkElement scopes (Controls, Panels, TextElements-derived elements).","In templates, place Interaction.Behaviors on the templated root element, not on non-visual DependencyObjects.","When wiring behaviors in code, type the host parameter as FrameworkElement to get compile-time safety."],"tags":["wpf","behavior","type-mismatch"],"backgroundTag":"type-mismatch","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"}