{"record":{"id":"6c5b83c9d0a85aa8","repo":"HandyOrg/HandyControl","slug":"exceptionstringtable-eventtriggerbase","errorCode":null,"errorMessage":"ExceptionStringTable.RetargetedTypeConstraintViolatedExceptionMessage","messagePattern":"ExceptionStringTable\\.RetargetedTypeConstraintViolatedExceptionMessage","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Shared/System.Windows.Interactivity/EventTriggerBase.cs","lineNumber":61,"sourceCode":"        {\n            if (string.IsNullOrEmpty(SourceName))\n                return ReadLocalValue(SourceNameProperty) != DependencyProperty.UnsetValue;\n            return true;\n        }\n    }\n\n    public object Source\n    {\n        get\n        {\n            object associatedObject = AssociatedObject;\n            if (SourceObject != null)\n                return SourceObject;\n            if (IsSourceNameSet)\n            {\n                associatedObject = SourceNameResolver.Object;\n                if (associatedObject != null && !SourceTypeConstraint.IsInstanceOfType(associatedObject))\n                    throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture,\n                        ExceptionStringTable.RetargetedTypeConstraintViolatedExceptionMessage, GetType().Name,\n                        associatedObject.GetType(), SourceTypeConstraint, \"Source\"));\n            }\n            return associatedObject;\n        }\n    }\n\n    public string SourceName\n    {\n        get =>\n            (string) GetValue(SourceNameProperty);\n        set => SetValue(SourceNameProperty, value);\n    }\n\n    private NameResolver SourceNameResolver { get; }\n\n    public object SourceObject\n    {","sourceCodeStart":43,"sourceCodeEnd":79,"githubUrl":"https://github.com/HandyOrg/HandyControl/blob/2c0875ebd67326e0c67282967e3e809c69282fee/src/Shared/System.Windows.Interactivity/EventTriggerBase.cs#L43-L79","documentation":"EventTriggerBase resolves its Source property; when a SourceName is set and the resolved object does not satisfy SourceTypeConstraint (from SourceTypeConstraintAttribute or the trigger's generic constraint), InvalidOperationException RetargetedTypeConstraintViolatedExceptionMessage is thrown naming the trigger type, actual object type, expected constraint, and property name.","triggerScenarios":"Setting EventTrigger.SourceName (or SourceObject indirectly) such that SourceNameResolver resolves an object that is not an instance of the trigger's SourceTypeConstraint, then accessing the Source property during attach/resolution.","commonSituations":"SourceName points to an element of the wrong type in XAML (e.g. a trigger expecting a Button but resolving a Grid); renaming elements so SourceName resolves to a different sibling; DataTemplate retargeting where the resolved element type changes.","solutions":["Ensure the element named in SourceName matches the trigger's source type constraint (check SourceTypeConstraint.IsInstanceOfType).","Correct the SourceName in XAML to point at an element of the expected type.","Add SourceTypeConstraintAttribute matching your actual source element type if the constraint is wrong.","Bind SourceObject explicitly to a compatible object instead of relying on name resolution."],"exampleFix":"<!-- before -->\n<i:EventTrigger SourceName=\"myGrid\" />\n<!-- after -->\n<i:EventTrigger SourceName=\"myButton\" />","handlingStrategy":"validation","validationCode":"var resolved = FindName(trigger.SourceName) as DependencyObject;\nif (resolved != null && !trigger.SourceTypeConstraint.IsInstanceOfType(resolved))\n    throw new InvalidOperationException($\"SourceName '{trigger.SourceName}' resolved to {resolved.GetType()} but constraint is {trigger.SourceTypeConstraint}\");","typeGuard":"static bool SourceSatisfiesConstraint(EventTriggerBase t, object resolved) =>\n    resolved == null || t.SourceTypeConstraint.IsInstanceOfType(resolved);","tryCatchPattern":"try { /* attach / access trigger.Source */ }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"RetargetedTypeConstraintViolated\")) {\n    // fix SourceName or widen SourceTypeConstraint\n}","preventionTips":["Keep SourceName pointing at elements of the expected type.","Re-verify SourceName after renames or template changes.","Declare SourceTypeConstraintAttribute matching your real source type.","Prefer explicit SourceObject bindings over fragile name resolution."],"tags":["wpf","interactivity","type-mismatch","eventtrigger","source-name"],"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"}