dotnet/wpf · error · InvalidOperationException

Storyboard_MediaElementNotFound

Storyboard_MediaElementNotFound

Error message

SR.Format(SR.Storyboard_MediaElementNotFound, currentObjectName)

What it means

ApplyMediaClock (called from ClockTreeWalkRecursive for media timelines) resolved the timeline's TargetName in the containing namescope, but the object found is not a MediaElement. MediaTimeline requires a MediaElement target, so an InvalidOperationException naming the target name is thrown.

Solutions

  1. Set Storyboard.TargetName of the MediaTimeline to the x:Name of a MediaElement (e.g. TargetName="player" where <MediaElement x:Name="player"/>).
  2. Verify the named element is actually a MediaElement, not merely a similarly named control.
  3. Alternatively set the target in code: Storyboard.SetTarget(mediaTimeline, player);

Example fix

<!-- before -->
<MediaElement x:Name="videoPlayer"/>
<MediaTimeline Source="intro.wmv" Storyboard.TargetName="video"/> <!-- 'video' is not a MediaElement -->

<!-- after -->
<MediaTimeline Source="intro.wmv" Storyboard.TargetName="videoPlayer"/>
Defensive patterns

Strategy: type-guard

Validate before calling

if (fe.FindName(mediaTimelineName) is not MediaElement)
    throw new InvalidOperationException($"'{mediaTimelineName}' does not resolve to a MediaElement");

Type guard

bool TargetIsMediaElement(FrameworkElement fe, string name) => fe.FindName(name) is MediaElement;

Try / catch

try { sb.Begin(fe); }
catch (InvalidOperationException ex) when (ex.Message.Contains(nameof(MediaElement))) {
    // fix: point MediaTimeline.TargetName at a MediaElement
}

Prevention

When it happens

Trigger: A MediaTimeline whose Storyboard.TargetName resolves via ResolveTargetName to something other than a MediaElement — e.g. an Image, Panel, or any other named element in the same namescope.

Common situations: Pointing MediaTimeline.TargetName at the wrong element; renaming a MediaElement and leaving a stale name on the timeline; confusing MediaElement's Name with the timeline's TargetName.

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 dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/cb3d3efe9502cb89. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Media/Animation/Storyboard.cs:542

    /// <summary>
    ///     When we've found a media clock, try to find a corresponding media
    /// element and attach the media clock to that element.
    /// </summary>
    private static void ApplyMediaClock( INameScope nameScope, DependencyObject containingObject,
        DependencyObject currentObject, string currentObjectName, MediaClock mediaClock )
    {
        MediaElement targetMediaElement = null;

        if( currentObjectName != null )
        {
            // Find the object named as the current target name.
            DependencyObject mentor = Helper.FindMentor(containingObject);
            targetMediaElement = ResolveTargetName(currentObjectName, nameScope, mentor ) as MediaElement;

            if( targetMediaElement == null )
            {
                throw new InvalidOperationException(SR.Format(SR.Storyboard_MediaElementNotFound, currentObjectName ));
            }
        }
        else if( currentObject != null )
        {
            targetMediaElement = currentObject as MediaElement;
        }
        else
        {
            targetMediaElement = containingObject as MediaElement;
        }

        if( targetMediaElement == null )
        {
            throw new InvalidOperationException(SR.Storyboard_MediaElementRequired);
        }

        targetMediaElement.Clock = mediaClock;
    }

View on GitHub (pinned to 81131a70a4)