{"record":{"id":"0d764b54385add92","repo":"HandyOrg/HandyControl","slug":"exceptionstringtable-typeconstraintviolatedexceptionmessage-0d764b","errorCode":null,"errorMessage":"ExceptionStringTable.TypeConstraintViolatedExceptionMessage","messagePattern":"ExceptionStringTable\\.TypeConstraintViolatedExceptionMessage","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Shared/System.Windows.Interactivity/TriggerBase.cs","lineNumber":58,"sourceCode":"    protected virtual Type AssociatedObjectTypeConstraint\n    {\n        get\n        {\n            ReadPreamble();\n            return _associatedObjectTypeConstraint;\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                    .CannotHostTriggerMultipleTimesExceptionMessage);\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            Actions.Attach(dependencyObject);\n            OnAttached();\n        }\n    }\n\n    public void Detach()\n    {\n        OnDetaching();\n        WritePreamble();\n        _associatedObject = null;\n        WritePostscript();\n        Actions.Detach();","sourceCodeStart":40,"sourceCodeEnd":76,"githubUrl":"https://github.com/HandyOrg/HandyControl/blob/2c0875ebd67326e0c67282967e3e809c69282fee/src/Shared/System.Windows.Interactivity/TriggerBase.cs#L40-L76","documentation":"TriggerBase.Attach throws InvalidOperationException (formatted via string.Format with trigger type, object type, and constraint type names) when the incoming dependencyObject does not satisfy AssociatedObjectTypeConstraint. Each trigger declares the type of object it can attach to (e.g. only UIElement); attaching to an incompatible type violates the type constraint.","triggerScenarios":"Attaching a trigger whose AssociatedTypeAttribute/type constraint is T to a DependencyObject that is not T (or not derived from T), e.g. attaching a trigger constrained to FrameworkElement to a plain DependencyObject.","commonSituations":"Custom triggers with a narrow AssociatedType constraint applied to broader elements in XAML; refactoring that changed the element type under a trigger; applying behaviors from a different control library with stricter constraints.","solutions":["Attach the trigger to an object whose type matches (or derives from) the trigger's AssociatedObjectTypeConstraint","Relax the trigger's type constraint (AssociatedTypeAttribute / generic constraint) to cover the target type","Use a different trigger designed for the target's type"],"exampleFix":"// before\nmyTrigger.Attach(unsupportedDependencyObject); // type not instance of constraint\n// after\nif (myTrigger.AssociatedObjectTypeConstraint.IsInstanceOfType(unsupportedDependencyObject))\n    myTrigger.Attach(unsupportedDependencyObject);","handlingStrategy":"type-guard","validationCode":"if (!trigger.AssociatedObjectTypeConstraint.IsInstanceOfType(target)) return;","typeGuard":"static bool CanAttach(TriggerBase t, DependencyObject o) => t.AssociatedObjectTypeConstraint.IsInstanceOfType(o);","tryCatchPattern":"try { trigger.Attach(target); } catch (InvalidOperationException ex) when (ex.Message.Contains(\"constraint\")) { Log.Warn(\"Type constraint violated\"); }","preventionTips":["Check the trigger's AssociatedType constraint first","Match trigger types to element types","Update constraints when refactoring element types"],"tags":["wpf","interactivity","type-mismatch"],"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"}