HandyOrg/HandyControl · error · InvalidOperationException

ExceptionStringTable.CannotHostTriggerCollectionMultipleTime…

Error message

ExceptionStringTable.CannotHostTriggerCollectionMultipleTimesExceptionMessage

What it means

Analogous to behaviors: a TriggerCollection already attached to an object (AssociatedObject != null) cannot be attached to another. Interaction.OnTriggersChanged throws InvalidOperationException CannotHostTriggerCollectionMultipleTimesExceptionMessage when the newly assigned trigger collection is already hosted.

Solutions

  1. Create a fresh TriggerCollection per host element.
  2. Detach/remove the collection from the old host first so its AssociatedObject resets.
  3. Use x:Shared="False" on shared XAML collection resources.
  4. Set collections individually instead of copying dependency property values between elements.

Example fix

// before
var triggers = new TriggerCollection { new EventTrigger { EventName = "Click" } };
Interaction.SetTriggers(button1, triggers);
Interaction.SetTriggers(button2, triggers); // throws
// after
Interaction.SetTriggers(button1, new TriggerCollection { new EventTrigger { EventName = "Click" } });
Interaction.SetTriggers(button2, new TriggerCollection { new EventTrigger { EventName = "Click" } });
Defensive patterns

Strategy: validation

Validate before calling

if (sharedTriggers.AssociatedObject != null)
    sharedTriggers = new TriggerCollection(sharedTriggers); // clone before re-hosting

Type guard

static bool CanHostTriggers(TriggerCollection c) => c.AssociatedObject == null;

Try / catch

try { Interaction.SetTriggers(element, triggers); }
catch (InvalidOperationException ex) when (ex.Message.Contains("TriggerCollectionMultipleTimes")) {
    // assign a fresh TriggerCollection instance
}

Prevention

When it happens

Trigger: Assigning a TriggerCollection instance whose AssociatedObject is set to a second element via Interaction.SetTriggers / the TriggersProperty change callback.

Common situations: Sharing one TriggerCollection across multiple controls in code; a shared XAML resource (collection) referenced by several elements; reparenting elements that carry the same collection instance.

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/449010c3cea47588. Report an issue: GitHub.

Appendix: source

Thrown at src/Shared/System.Windows.Interactivity/Interaction.cs:67

            return;
        if (newValue.AssociatedObject != null)
            throw new InvalidOperationException(ExceptionStringTable
                .CannotHostBehaviorCollectionMultipleTimesExceptionMessage);
        newValue.Attach(obj);
    }

    private static void OnTriggersChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
    {
        var oldValue = args.OldValue as TriggerCollection;
        var newValue = args.NewValue as TriggerCollection;
        if (Equals(oldValue, newValue))
            return;
        if (oldValue?.AssociatedObject != null)
            oldValue.Detach();
        if (newValue == null || obj == null)
            return;
        if (newValue.AssociatedObject != null)
            throw new InvalidOperationException(ExceptionStringTable
                .CannotHostTriggerCollectionMultipleTimesExceptionMessage);
        newValue.Attach(obj);
    }

    internal static bool IsElementLoaded(FrameworkElement element)
    {
        return element.IsLoaded;
    }
}

View on GitHub (pinned to 2c0875ebd6)