stride3d/stride · error · InvalidOperationException

The OnComboBoxClosedWithSelectionBehavior must be attached…

Error message

The OnComboBoxClosedWithSelectionBehavior must be attached to a ComboBox.

What it means

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.

Solutions

  1. Move the behavior declaration onto a ComboBox element in XAML
  2. If the target is not a ComboBox, use the appropriate behavior for that control type
  3. For programmatic attachment, check 'if (target is ComboBox)' before adding the behavior

Example fix

// before
<TextBox>
  <i:Interaction.Behaviors>
    <b:OnComboBoxClosedWithSelectionBehavior/>
  </i:Interaction.Behaviors>
</TextBox>
// after
<ComboBox>
  <i:Interaction.Behaviors>
    <b:OnComboBoxClosedWithSelectionBehavior/>
  </i:Interaction.Behaviors>
</ComboBox>
Defensive patterns

Strategy: type-guard

Validate before calling

if (element is ComboBox) element.AddBehavior(new OnComboBoxClosedWithSelectionBehavior());

Type guard

static bool CanAttachComboBoxClosedBehavior(object o) => o is ComboBox;

Try / catch

try { behaviors.Add(new OnComboBoxClosedWithSelectionBehavior()); }
catch (InvalidOperationException ex) { log.Warn($"Wrong AssociatedObject: {ex.Message}"); }

Prevention

When it happens

Trigger: Attaching the behavior (via Interaction.Behaviors in XAML or code) to anything other than a ComboBox, e.g. a TextBox, DataGrid, or plain Control.

Common situations: 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.

Understand the failure class

Background: "is not a compatible type" / "cannot merge" errors: when a value's type doesn't match what the library requires — this error's family across 65 libraries.

Related errors


AI-assisted analysis of stride3d/stride@96fad776d2 (2026-09-14). Data as JSON: /api/errors/ce3b5c8887eb69da. Report an issue: GitHub.

Appendix: source

Thrown at sources/editor/Stride.Core.Assets.Editor/View/Behaviors/OnComboBoxClosedWithSelectionBehavior.cs:20

// Distributed under the MIT license. See the LICENSE.md file in the project root for more information.
using System;
using System.Windows.Controls;

using Stride.Core.Presentation.Behaviors;

namespace Stride.Core.Assets.Editor.View.Behaviors
{
    public class OnComboBoxClosedWithSelectionBehavior : OnEventCommandBehavior
    {
        public OnComboBoxClosedWithSelectionBehavior()
        {
            EventName = "DropDownClosed";
        }

        protected override void OnAttached()
        {
            if (!(AssociatedObject is ComboBox))
                throw new InvalidOperationException("The OnComboBoxClosedWithSelectionBehavior must be attached to a ComboBox.");

            base.OnAttached();
        }

        protected override void OnEvent()
        {
            var comboBox = (ComboBox)AssociatedObject;
            if (comboBox.SelectedItem != null)
                base.OnEvent();
        }
    }
}

View on GitHub (pinned to 96fad776d2)