{"record":{"id":"ce3b5c8887eb69da","repo":"stride3d/stride","slug":"the-oncomboboxclosedwithselectionbehavior-must-be-attached","errorCode":null,"errorMessage":"The OnComboBoxClosedWithSelectionBehavior must be attached to a ComboBox.","messagePattern":"The OnComboBoxClosedWithSelectionBehavior must be attached to a ComboBox\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"sources/editor/Stride.Core.Assets.Editor/View/Behaviors/OnComboBoxClosedWithSelectionBehavior.cs","lineNumber":20,"sourceCode":"// Distributed under the MIT license. See the LICENSE.md file in the project root for more information.\nusing System;\nusing System.Windows.Controls;\n\nusing Stride.Core.Presentation.Behaviors;\n\nnamespace Stride.Core.Assets.Editor.View.Behaviors\n{\n    public class OnComboBoxClosedWithSelectionBehavior : OnEventCommandBehavior\n    {\n        public OnComboBoxClosedWithSelectionBehavior()\n        {\n            EventName = \"DropDownClosed\";\n        }\n\n        protected override void OnAttached()\n        {\n            if (!(AssociatedObject is ComboBox))\n                throw new InvalidOperationException(\"The OnComboBoxClosedWithSelectionBehavior must be attached to a ComboBox.\");\n\n            base.OnAttached();\n        }\n\n        protected override void OnEvent()\n        {\n            var comboBox = (ComboBox)AssociatedObject;\n            if (comboBox.SelectedItem != null)\n                base.OnEvent();\n        }\n    }\n}\n","sourceCodeStart":2,"sourceCodeEnd":33,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/editor/Stride.Core.Assets.Editor/View/Behaviors/OnComboBoxClosedWithSelectionBehavior.cs#L2-L33","documentation":"OnComboBoxClosedWithSelectionBehavior checks in OnAttached that the AssociatedObject is a ComboBox because its OnEvent handler relies on the ComboBox DropDownClosed event and its SelectedItem semantics. Attaching it to any other element throws InvalidOperationException immediately.","triggerScenarios":"Attaching the behavior (via Interaction.Behaviors in XAML or code) to anything other than a ComboBox, e.g. a TextBox, DataGrid, or plain Control.","commonSituations":"Copy-paste of behavior markup between controls; refactor renaming a ComboBox to another control while leaving the behavior attached; generic event-trigger behaviors reused on the wrong element.","solutions":["Move the behavior declaration onto a ComboBox element in XAML","If the target is not a ComboBox, use the appropriate behavior for that control type","For programmatic attachment, check 'if (target is ComboBox)' before adding the behavior"],"exampleFix":"// before\n<TextBox>\n  <i:Interaction.Behaviors>\n    <b:OnComboBoxClosedWithSelectionBehavior/>\n  </i:Interaction.Behaviors>\n</TextBox>\n// after\n<ComboBox>\n  <i:Interaction.Behaviors>\n    <b:OnComboBoxClosedWithSelectionBehavior/>\n  </i:Interaction.Behaviors>\n</ComboBox>","handlingStrategy":"type-guard","validationCode":"if (element is ComboBox) element.AddBehavior(new OnComboBoxClosedWithSelectionBehavior());","typeGuard":"static bool CanAttachComboBoxClosedBehavior(object o) => o is ComboBox;","tryCatchPattern":"try { behaviors.Add(new OnComboBoxClosedWithSelectionBehavior()); }\ncatch (InvalidOperationException ex) { log.Warn($\"Wrong AssociatedObject: {ex.Message}\"); }","preventionTips":["Verify the behavior's documented target control before reuse","Keep behavior XAML co-located with its target control type","Use code-behind attachment with a type check when unsure"],"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"}