{"record":{"id":"cb3d3efe9502cb89","repo":"dotnet/wpf","slug":"storyboard-mediaelementnotfound","errorCode":"Storyboard_MediaElementNotFound","errorMessage":"SR.Format(SR.Storyboard_MediaElementNotFound, currentObjectName)","messagePattern":"SR\\.Format\\(SR\\.Storyboard_MediaElementNotFound, currentObjectName\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Media/Animation/Storyboard.cs","lineNumber":542,"sourceCode":"\n    /// <summary>\n    ///     When we've found a media clock, try to find a corresponding media\n    /// element and attach the media clock to that element.\n    /// </summary>\n    private static void ApplyMediaClock( INameScope nameScope, DependencyObject containingObject,\n        DependencyObject currentObject, string currentObjectName, MediaClock mediaClock )\n    {\n        MediaElement targetMediaElement = null;\n\n        if( currentObjectName != null )\n        {\n            // Find the object named as the current target name.\n            DependencyObject mentor = Helper.FindMentor(containingObject);\n            targetMediaElement = ResolveTargetName(currentObjectName, nameScope, mentor ) as MediaElement;\n\n            if( targetMediaElement == null )\n            {\n                throw new InvalidOperationException(SR.Format(SR.Storyboard_MediaElementNotFound, currentObjectName ));\n            }\n        }\n        else if( currentObject != null )\n        {\n            targetMediaElement = currentObject as MediaElement;\n        }\n        else\n        {\n            targetMediaElement = containingObject as MediaElement;\n        }\n\n        if( targetMediaElement == null )\n        {\n            throw new InvalidOperationException(SR.Storyboard_MediaElementRequired);\n        }\n\n        targetMediaElement.Clock = mediaClock;\n    }","sourceCodeStart":524,"sourceCodeEnd":560,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Media/Animation/Storyboard.cs#L524-L560","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Set Storyboard.TargetName of the MediaTimeline to the x:Name of a MediaElement (e.g. TargetName=\"player\" where <MediaElement x:Name=\"player\"/>).","Verify the named element is actually a MediaElement, not merely a similarly named control.","Alternatively set the target in code: Storyboard.SetTarget(mediaTimeline, player);"],"exampleFix":"<!-- before -->\n<MediaElement x:Name=\"videoPlayer\"/>\n<MediaTimeline Source=\"intro.wmv\" Storyboard.TargetName=\"video\"/> <!-- 'video' is not a MediaElement -->\n\n<!-- after -->\n<MediaTimeline Source=\"intro.wmv\" Storyboard.TargetName=\"videoPlayer\"/>","handlingStrategy":"type-guard","validationCode":"if (fe.FindName(mediaTimelineName) is not MediaElement)\n    throw new InvalidOperationException($\"'{mediaTimelineName}' does not resolve to a MediaElement\");","typeGuard":"bool TargetIsMediaElement(FrameworkElement fe, string name) => fe.FindName(name) is MediaElement;","tryCatchPattern":"try { sb.Begin(fe); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(nameof(MediaElement))) {\n    // fix: point MediaTimeline.TargetName at a MediaElement\n}","preventionTips":["Point MediaTimeline TargetName only at MediaElement x:Name.","Re-verify TargetName after renaming MediaElements.","Prefer Storyboard.SetTarget(timeline, mediaElement) in code for compile-time safety."],"tags":["wpf","mediaelement","mediatimeline","storyboard","type-mismatch"],"backgroundTag":"type-mismatch","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}