HandyOrg/HandyControl · error · InvalidOperationException

ExceptionStringTable.CannotHostTriggerMultipleTimesException…

Error message

ExceptionStringTable.CannotHostTriggerMultipleTimesExceptionMessage

What it means

TriggerBase.Attach throws InvalidOperationException when the trigger already has a non-null AssociatedObject and Attach is called with a different object. A trigger can only ever be hosted on one associated object for its lifetime. This is the Interactivity library enforcing single-host semantics for triggers.

Solutions

  1. Create a new trigger instance per attached object instead of reusing one
  2. Detach the trigger from its current object first (only possible if the API allows) before attaching to another
  3. Declare triggers inline per control rather than as shared resources

Example fix

// before
var trigger = (EventTrigger)FindResource("sharedTrigger");
trigger.Attach(grid1);
trigger.Attach(grid2); // throws
// after
grid1.Triggers.Add(new EventTrigger { ... });
grid2.Triggers.Add(new EventTrigger { ... });
Defensive patterns

Strategy: validation

Validate before calling

if (trigger.AssociatedObject != null) trigger = CreateNewTrigger();
trigger.Attach(target);

Try / catch

try { trigger.Attach(target); } catch (InvalidOperationException ex) when (ex.Message.Contains("host")) { trigger = CreateNewTrigger(); trigger.Attach(target); }

Prevention

When it happens

Trigger: Calling trigger.Attach(newObject) on a trigger already attached elsewhere; adding an already-hosted trigger to another TriggerCollection (via ItemAdded); reusing a trigger instance defined as a shared resource across multiple objects.

Common situations: Reusing a Trigger (e.g. EventTrigger) instance across several controls; moving a behavior/trigger from one control to another at runtime; XAML resource sharing pitfalls (x:Shared).

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


AI-assisted analysis of HandyOrg/HandyControl@2c0875ebd6 (2026-09-14). Data as JSON: /api/errors/17ba051429180b85. Report an issue: GitHub.

Appendix: source

Thrown at src/Shared/System.Windows.Interactivity/TriggerBase.cs:54

            return _associatedObject;
        }
    }

    protected virtual Type AssociatedObjectTypeConstraint
    {
        get
        {
            ReadPreamble();
            return _associatedObjectTypeConstraint;
        }
    }

    public void Attach(DependencyObject dependencyObject)
    {
        if (!Equals(dependencyObject, AssociatedObject))
        {
            if (AssociatedObject != null)
                throw new InvalidOperationException(ExceptionStringTable
                    .CannotHostTriggerMultipleTimesExceptionMessage);
            if (dependencyObject != null &&
                !AssociatedObjectTypeConstraint.IsInstanceOfType(dependencyObject))
                throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture,
                    ExceptionStringTable.TypeConstraintViolatedExceptionMessage,
                    new object[]
                        {GetType().Name, dependencyObject.GetType().Name, AssociatedObjectTypeConstraint.Name}));
            WritePreamble();
            _associatedObject = dependencyObject;
            WritePostscript();
            Actions.Attach(dependencyObject);
            OnAttached();
        }
    }

    public void Detach()
    {
        OnDetaching();

View on GitHub (pinned to 2c0875ebd6)