dotnet/wpf · error · ArgumentException

SR.Format(SR.TargetNameNotFound, targetName)

Error message

SR.Format(SR.TargetNameNotFound, targetName)

What it means

FindNamedFrameworkElement throws ArgumentException when no element with the requested x:Name/Name can be found by walking the logical tree from the start element (LogicalTreeHelper.FindLogicalNode returns null). This happens while resolving a TemplateBinding/animatable target by name; the given TargetName simply does not exist under that starting element.

Solutions

  1. Correct the TargetName string to exactly match the x:Name/Name of an element in the same logical tree/namescope
  2. Ensure the storyboard and its target are in the same XAML namescope (define both in the same template/control)
  3. Verify the target element exists at the time of resolution (not removed by conditional template logic)
  4. Use FindName on the templated parent/template root to confirm the name resolves before applying the animation

Example fix

// before
<Storyboard>
  <DoubleAnimation Storyboard.TargetName="TxtBox" Storyboard.TargetProperty="Opacity" /> <!-- no 'TxtBox' exists -->
</Storyboard>

// after
<Storyboard>
  <DoubleAnimation Storyboard.TargetName="MyTextBox" Storyboard.TargetProperty="Opacity" /> <!-- matches x:Name="MyTextBox" -->
</Storyboard>
Defensive patterns

Strategy: validation

Validate before calling

var target = startElement.FindName(targetName)
             ?? LogicalTreeHelper.FindLogicalNode(startElement, targetName);
if (target == null)
    throw new ArgumentException($"TargetName '{targetName}' not found in logical tree");

Type guard

static bool TargetExists(DependencyObject root, string name) =>
    LogicalTreeHelper.FindLogicalNode(root, name) != null;

Try / catch

try { ResolveNamedTarget(startElement, targetName); }
catch (ArgumentException ex) { log.Error($"Storyboard target '{targetName}' missing", ex); }

Prevention

When it happens

Trigger: Resolving a named target (e.g., Storyboard.TargetName, template trigger target) where targetName does not match any element in the logical tree beneath startElement.

Common situations: Storyboard TargetName referencing an element that is not in the same template/namescope; typo in the name; the target element was renamed or removed; applying a template-defined storyboard from outside the template where namescopes differ.

Understand the failure class

Background: 'Could not be found', 'does not exist', 'not found in database': the resource-not-found family when an ID, slug, key, or URI lookup comes back empty — this error's family across 20 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/1e7b9fa8c8f08183. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/FrameworkElement.cs:477

        // If the name is found on a FrameworkContentElement, an exception is thrown
        // If the name is not found attached to anything, an exception is thrown
        internal static FrameworkElement FindNamedFrameworkElement( FrameworkElement startElement, string targetName )
        {
            FrameworkElement targetFE = null;

            if( targetName == null || targetName.Length == 0 )
            {
                targetFE = startElement;
            }
            else
            {
                DependencyObject targetObject = null;

                targetObject = LogicalTreeHelper.FindLogicalNode( startElement, targetName );

                if( targetObject == null )
                {
                    throw new ArgumentException( SR.Format(SR.TargetNameNotFound, targetName));
                }

                FrameworkObject fo = new FrameworkObject(targetObject);
                if( fo.IsFE )
                {
                    targetFE = fo.FE;
                }
                else
                {
                    throw new InvalidOperationException(SR.Format(SR.NamedObjectMustBeFrameworkElement, targetName));
                }
            }

            return targetFE;
        }

        /// <summary>
        ///     Triggers associated with this object.  Both the triggering condition

View on GitHub (pinned to 81131a70a4)