{"record":{"id":"ce4a54205e33946c","repo":"HandyOrg/HandyControl","slug":"exceptionstringtable-typeconstraintviolatedexceptionmessage-ce4a54","errorCode":null,"errorMessage":"ExceptionStringTable.TypeConstraintViolatedExceptionMessage","messagePattern":"ExceptionStringTable\\.TypeConstraintViolatedExceptionMessage","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Shared/System.Windows.Interactivity/TriggerAction.cs","lineNumber":75,"sourceCode":"        }\n        set\n        {\n            WritePreamble();\n            _isHosted = value;\n            WritePostscript();\n        }\n    }\n\n    public void Attach(DependencyObject dependencyObject)\n    {\n        if (!Equals(dependencyObject, AssociatedObject))\n        {\n            if (AssociatedObject != null)\n                throw new InvalidOperationException(ExceptionStringTable\n                    .CannotHostTriggerActionMultipleTimesExceptionMessage);\n            if (dependencyObject != null &&\n                !AssociatedObjectTypeConstraint.IsInstanceOfType(dependencyObject))\n                throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture,\n                    ExceptionStringTable.TypeConstraintViolatedExceptionMessage,\n                    new object[]\n                        {GetType().Name, dependencyObject.GetType().Name, AssociatedObjectTypeConstraint.Name}));\n            WritePreamble();\n            _associatedObject = dependencyObject;\n            WritePostscript();\n            OnAttached();\n        }\n    }\n\n    public void Detach()\n    {\n        OnDetaching();\n        WritePreamble();\n        _associatedObject = null;\n        WritePostscript();\n    }\n","sourceCodeStart":57,"sourceCodeEnd":93,"githubUrl":"https://github.com/HandyOrg/HandyControl/blob/2c0875ebd67326e0c67282967e3e809c69282fee/src/Shared/System.Windows.Interactivity/TriggerAction.cs#L57-L93","documentation":"TriggerAction.Attach validates the host against AssociatedObjectTypeConstraint (from AssociatedObjectTypeConstraintAttribute or the trigger's generic constraint). Attaching a DependencyObject that is not an instance of that constraint throws InvalidOperationException TypeConstraintViolatedExceptionMessage naming the action type, actual object type, and expected constraint type.","triggerScenarios":"Calling action.Attach(obj) where obj is non-null and !AssociatedObjectTypeConstraint.IsInstanceOfType(obj), e.g. adding a TargetedTriggerAction constrained to FrameworkElement onto a non-FrameworkElement host, via ItemAdded or direct Attach.","commonSituations":"Using an action designed for a specific element type (e.g. storyboard actions requiring FrameworkElement) on an incompatible host; XAML attaching an action to a wrong parent; constraint attributes tightened by a library upgrade while existing markup unchanged.","solutions":["Add/attach the action only to objects satisfying its AssociatedObjectTypeConstraint (check IsInstanceOfType first).","Move the action to a compatible trigger/host element.","Adjust AssociatedObjectTypeConstraintAttribute if the constraint is too strict for your scenario.","Use the generic typed action variant matching your host element type."],"exampleFix":"// before\nnonFrameworkObject.SetValue(Interaction.BehaviorsProperty,\n    new BehaviorCollection { new Behavior { Actions = { myFrameworkElementAction } } }); // throws\n// after\nif (action.AssociatedObjectTypeConstraint.IsInstanceOfType(host))\n    action.Attach(host);","handlingStrategy":"validation","validationCode":"if (host == null || !action.AssociatedObjectTypeConstraint.IsInstanceOfType(host))\n    throw new InvalidOperationException($\"{host?.GetType().Name} violates {action.AssociatedObjectTypeConstraint.Name} constraint\");","typeGuard":"static bool MatchesActionConstraint(TriggerAction a, DependencyObject host) =>\n    host != null && a.AssociatedObjectTypeConstraint.IsInstanceOfType(host);","tryCatchPattern":"try { action.Attach(host); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"TypeConstraintViolated\")) {\n    // move the action to a compatible host element\n}","preventionTips":["Verify host element type against AssociatedObjectTypeConstraintAttribute.","Use the generic typed action matching the host element type.","Re-check constraints after library upgrades that narrow them.","Guard with IsInstanceOfType when attaching actions in code."],"tags":["wpf","interactivity","type-mismatch","trigger-action","type-constraint"],"backgroundTag":"type-mismatch","analyzedSha":"2c0875ebd67326e0c67282967e3e809c69282fee","analyzedAt":"2026-09-14T14:45:29.754Z","contentChangedAt":"2026-09-14T14:45:29.754Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}