{"record":{"id":"18d13a9b566a4d5f","repo":"stride3d/stride","slug":"the-onselectionchangedwithselectionbehavior-must-be-attached","errorCode":null,"errorMessage":"The OnSelectionChangedWithSelectionBehavior must be attached to a Selector.","messagePattern":"The OnSelectionChangedWithSelectionBehavior must be attached to a Selector\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"sources/editor/Stride.Core.Assets.Editor/View/Behaviors/OnSelectionChangedWithSelectionBehavior.cs","lineNumber":19,"sourceCode":"// Copyright (c) .NET Foundation and Contributors (https://dotnetfoundation.org/ & https://stride3d.net) and Silicon Studio Corp. (https://www.siliconstudio.co.jp)\n// Distributed under the MIT license. See the LICENSE.md file in the project root for more information.\nusing System;\nusing System.Windows.Controls.Primitives;\nusing Stride.Core.Presentation.Behaviors;\n\nnamespace Stride.Core.Assets.Editor.View.Behaviors\n{\n    public class OnSelectionChangedWithSelectionBehavior : OnEventCommandBehavior\n    {\n        public OnSelectionChangedWithSelectionBehavior()\n        {\n            EventName = \"SelectionChanged\";\n        }\n\n        protected override void OnAttached()\n        {\n            if (!(AssociatedObject is Selector))\n                throw new InvalidOperationException(\"The OnSelectionChangedWithSelectionBehavior must be attached to a Selector.\");\n\n            base.OnAttached();\n        }\n\n        protected override void OnEvent()\n        {\n            var selector = (Selector)AssociatedObject;\n            if (selector.SelectedItem != null)\n                base.OnEvent();\n        }\n    }\n}\n","sourceCodeStart":1,"sourceCodeEnd":32,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/editor/Stride.Core.Assets.Editor/View/Behaviors/OnSelectionChangedWithSelectionBehavior.cs#L1-L32","documentation":"OnSelectionChangedWithSelectionBehavior validates in OnAttached that the AssociatedObject derives from Selector (ListBox, ComboBox, etc.), since it hooks the SelectionChanged routed event and reads selector-specific selection state. Any other target throws InvalidOperationException.","triggerScenarios":"Attaching the behavior to a non-Selector control such as a DataGrid (DataGrid is not a Selector subclass path expected here), TreeView, ItemsControl, or any FrameworkElement.","commonSituations":"Reusing a selection-change behavior snippet on DataGrid or ListView groups; moving the behavior during a XAML refactor so it lands on the parent ItemsControl.","solutions":["Attach the behavior to a Selector-derived control (ListBox, ComboBox, ListView)","For DataGrid, use a DataGrid-specific selection behavior instead","Guard programmatic attachment with 'if (obj is Selector)' before adding"],"exampleFix":"// before\n<ItemsControl>\n  <i:Interaction.Behaviors>\n    <b:OnSelectionChangedWithSelectionBehavior/>\n  </i:Interaction.Behaviors>\n// after\n<ListBox>\n  <i:Interaction.Behaviors>\n    <b:OnSelectionChangedWithSelectionBehavior/>\n  </i:Interaction.Behaviors>\n</ListBox>","handlingStrategy":"type-guard","validationCode":"if (control is Selector) control.AddBehavior(new OnSelectionChangedWithSelectionBehavior());","typeGuard":"static bool CanAttachSelectionChangedBehavior(object o) => o is Selector;","tryCatchPattern":"try { behaviors.Add(new OnSelectionChangedWithSelectionBehavior()); }\ncatch (InvalidOperationException ex) { log.Warn(\"Attach target must derive from Selector\", ex); }","preventionTips":["Remember DataGrid/TreeView are not Selector — use matching behaviors","Attach via ItemContainerStyle or on Selector-derived controls only","Type-check programmatically before adding behaviors"],"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"}