{"record":{"id":"813636e13130335f","repo":"stride3d/stride","slug":"this-behavior-must-be-attached-to-an-instance-of","errorCode":null,"errorMessage":"This behavior must be attached to an instance of TreeViewItem.","messagePattern":"This behavior must be attached to an instance of TreeViewItem\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"sources/editor/Stride.Core.Assets.Editor/View/Behaviors/TreeViewStopEditOnLostFocusBehavior.cs","lineNumber":19,"sourceCode":"protected override void OnAttached()\n{\n    if (!(AssociatedObject is TreeViewItem))\n        throw new InvalidOperationException(\"This behavior must be attached to an instance of TreeViewItem.\");\n    base.OnAttached();\n}\n\nprotected override void OnEvent()\n{\n    var treeViewItem = (TreeViewItem)AssociatedObject;\n    treeViewItem.SetCurrentValue(TreeViewItem.IsEditingProperty, false);\n}","sourceCodeStart":null,"sourceCodeEnd":null,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/editor/Stride.Core.Assets.Editor/View/Behaviors/TreeViewStopEditOnLostFocusBehavior.cs#L19","documentation":"TreeViewStopEditOnLostFocusBehavior overrides OnAttached and validates that the AssociatedObject (the element the behavior is attached to in XAML) is a TreeViewItem. Attaching it to any other control type throws InvalidOperationException because the behavior's logic depends on TreeViewItem-specific focus/selection semantics.","triggerScenarios":"Declaring the behavior in an Interaction.Behaviors collection on an element that is not a TreeViewItem (e.g. on TreeView itself, a Grid, or a ListBoxItem).","commonSituations":"Copy-pasting the behavior XAML snippet onto the wrong element; attaching at the TreeView level instead of via ItemContainerStyle for the TreeViewItem containers.","solutions":["Attach the behavior only to TreeViewItem elements, typically via <TreeView.ItemContainerStyle> with a Style targeting TreeViewItem containing <i:Interaction.Behaviors>","Move the behavior declaration to the correct element in the XAML tree","If attaching programmatically, verify AssociatedObject type before behaviors.Add"],"exampleFix":"// before\n<TreeView>\n  <i:Interaction.Behaviors>\n    <b:TreeViewStopEditOnLostFocusBehavior/>\n  </i:Interaction.Behaviors>\n// after\n<TreeView.ItemContainerStyle>\n  <Style TargetType=\"TreeViewItem\">\n    <Setter Property=\"b:TreeViewStopEditOnLostFocusBehavior.EnableStopEdit\" Value=\"True\"/>\n  </Style>\n</TreeView.ItemContainerStyle>","handlingStrategy":"type-guard","validationCode":"if (treeViewItem is TreeViewItem)\n    ((TreeViewItem)treeViewItem).AssureBehavior<TreeViewStopEditOnLostFocusBehavior>();","typeGuard":"static bool CanAttachStopEdit(object o) => o is TreeViewItem;","tryCatchPattern":"try { behaviors.Add(new TreeViewStopEditOnLostFocusBehavior()); }\ncatch (InvalidOperationException ex) { log.Warn(\"Behavior attached to wrong element type\", ex); }","preventionTips":["Attach item behaviors via ItemContainerStyle, not on the parent TreeView","Check the behavior's AssociatedObject type contract before wiring XAML","Review copy-pasted behavior blocks during XAML refactors"],"tags":["wpf","behavior","xaml"],"backgroundTag":"incompatible-source-type","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"}