stride3d/stride · error · InvalidOperationException

The OnSelectionChangedWithSelectionBehavior must be…

Error message

The OnSelectionChangedWithSelectionBehavior must be attached to a Selector.

What it means

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.

Solutions

  1. Attach the behavior to a Selector-derived control (ListBox, ComboBox, ListView)
  2. For DataGrid, use a DataGrid-specific selection behavior instead
  3. Guard programmatic attachment with 'if (obj is Selector)' before adding

Example fix

// before
<ItemsControl>
  <i:Interaction.Behaviors>
    <b:OnSelectionChangedWithSelectionBehavior/>
  </i:Interaction.Behaviors>
// after
<ListBox>
  <i:Interaction.Behaviors>
    <b:OnSelectionChangedWithSelectionBehavior/>
  </i:Interaction.Behaviors>
</ListBox>
Defensive patterns

Strategy: type-guard

Validate before calling

if (control is Selector) control.AddBehavior(new OnSelectionChangedWithSelectionBehavior());

Type guard

static bool CanAttachSelectionChangedBehavior(object o) => o is Selector;

Try / catch

try { behaviors.Add(new OnSelectionChangedWithSelectionBehavior()); }
catch (InvalidOperationException ex) { log.Warn("Attach target must derive from Selector", ex); }

Prevention

When it happens

Trigger: 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.

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

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/18d13a9b566a4d5f. Report an issue: GitHub.

Appendix: source

Thrown at sources/editor/Stride.Core.Assets.Editor/View/Behaviors/OnSelectionChangedWithSelectionBehavior.cs:19

// Copyright (c) .NET Foundation and Contributors (https://dotnetfoundation.org/ & https://stride3d.net) and Silicon Studio Corp. (https://www.siliconstudio.co.jp)
// Distributed under the MIT license. See the LICENSE.md file in the project root for more information.
using System;
using System.Windows.Controls.Primitives;
using Stride.Core.Presentation.Behaviors;

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

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

            base.OnAttached();
        }

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

View on GitHub (pinned to 96fad776d2)