HandyOrg/HandyControl · error · InvalidOperationException

ExceptionStringTable.RetargetedTypeConstraintViolatedExcepti…

Error message

ExceptionStringTable.RetargetedTypeConstraintViolatedExceptionMessage

What it means

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.

Solutions

  1. Ensure the element named in SourceName matches the trigger's source type constraint (check SourceTypeConstraint.IsInstanceOfType).
  2. Correct the SourceName in XAML to point at an element of the expected type.
  3. Add SourceTypeConstraintAttribute matching your actual source element type if the constraint is wrong.
  4. Bind SourceObject explicitly to a compatible object instead of relying on name resolution.

Example fix

<!-- before -->
<i:EventTrigger SourceName="myGrid" />
<!-- after -->
<i:EventTrigger SourceName="myButton" />
Defensive patterns

Strategy: validation

Validate before calling

var resolved = FindName(trigger.SourceName) as DependencyObject;
if (resolved != null && !trigger.SourceTypeConstraint.IsInstanceOfType(resolved))
    throw new InvalidOperationException($"SourceName '{trigger.SourceName}' resolved to {resolved.GetType()} but constraint is {trigger.SourceTypeConstraint}");

Type guard

static bool SourceSatisfiesConstraint(EventTriggerBase t, object resolved) =>
    resolved == null || t.SourceTypeConstraint.IsInstanceOfType(resolved);

Try / catch

try { /* attach / access trigger.Source */ }
catch (InvalidOperationException ex) when (ex.Message.Contains("RetargetedTypeConstraintViolated")) {
    // fix SourceName or widen SourceTypeConstraint
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


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

Appendix: source

Thrown at src/Shared/System.Windows.Interactivity/EventTriggerBase.cs:61

        {
            if (string.IsNullOrEmpty(SourceName))
                return ReadLocalValue(SourceNameProperty) != DependencyProperty.UnsetValue;
            return true;
        }
    }

    public object Source
    {
        get
        {
            object associatedObject = AssociatedObject;
            if (SourceObject != null)
                return SourceObject;
            if (IsSourceNameSet)
            {
                associatedObject = SourceNameResolver.Object;
                if (associatedObject != null && !SourceTypeConstraint.IsInstanceOfType(associatedObject))
                    throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture,
                        ExceptionStringTable.RetargetedTypeConstraintViolatedExceptionMessage, GetType().Name,
                        associatedObject.GetType(), SourceTypeConstraint, "Source"));
            }
            return associatedObject;
        }
    }

    public string SourceName
    {
        get =>
            (string) GetValue(SourceNameProperty);
        set => SetValue(SourceNameProperty, value);
    }

    private NameResolver SourceNameResolver { get; }

    public object SourceObject
    {

View on GitHub (pinned to 2c0875ebd6)