{"record":{"id":"910596a0066d86b7","repo":"dotnet/wpf","slug":"storyboard-mediaelementrequired","errorCode":"Storyboard_MediaElementRequired","errorMessage":"SR.Storyboard_MediaElementRequired","messagePattern":"SR\\.Storyboard_MediaElementRequired","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Media/Animation/Storyboard.cs","lineNumber":556,"sourceCode":"            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    }\n\n\n    /// <summary>\n    ///     Given an animation clock, add it to the data structure which tracks\n    /// all the clocks along with their associated target object and property.\n    /// </summary>\n    private static void UpdateMappings(\n        HybridDictionary clockMappings,\n        ObjectPropertyPair mappingKey,\n        AnimationClock animationClock)\n    {\n        object mappedObject = clockMappings[mappingKey];\n\n        Debug.Assert( mappedObject == null || mappedObject is AnimationClock || mappedObject is List<AnimationClock>,","sourceCodeStart":538,"sourceCodeEnd":574,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Media/Animation/Storyboard.cs#L538-L574","documentation":"Storyboard.Apply or a running MediaTimeline needs somewhere to attach the resulting MediaClock. When the storyboard targets a MediaTimeline, the resolved target (or the storyboard's ContainingObject) must be a MediaElement; ApplyMediaClock throws this InvalidOperationException when both come back null.","triggerScenarios":"Calling Storyboard.Apply or Begin on a storyboard whose children are MediaTimelines while TargetName/Target resolves to a non-MediaElement (or nothing), and the storyboard has no MediaElement ContainingObject.","commonSituations":"Targeting a MediaTimeline at an Image or Canvas by mistake; wrapping media in a ContentControl/Viewbox so the target is a container instead of the MediaElement; forgetting to set Storyboard.TargetName so the containing-object fallback is also not a MediaElement.","solutions":["Set Storyboard.TargetName/Storyboard.Target to an actual MediaElement in the storyboard's children (the MediaTimeline).","Ensure the MediaElement is the direct target, not a wrapping control; give the MediaElement its own x:Name and target that.","If applying manually, pass a MediaElement as the containingObject argument to Storyboard.Apply.","Register the target name via storyboard.RegisterName so TargetName resolution succeeds.","Replace the timeline with a non-media animation if the target was never meant to be a media element."],"exampleFix":"// before\n<Storyboard>\n  <MediaTimeline Source=\"a.wmv\" Storyboard.TargetName=\"mediaBorder\" />\n</Storyboard>\n<!-- after -->\n<Storyboard>\n  <MediaTimeline Source=\"a.wmv\" Storyboard.TargetName=\"mediaElement\" />\n</Storyboard>","handlingStrategy":"validation","validationCode":"static bool IsMediaStoryboardTarget(Storyboard sb, FrameworkElement container, string targetName)\n{\n    var target = targetName != null ? container.FindName(targetName) : container;\n    return target is MediaElement;\n}","typeGuard":"static bool IsMediaElement(object o) => o is MediaElement media && media.Clock != null || o is MediaElement;","tryCatchPattern":"try { storyboard.Begin(containingObject); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"MediaElement\"))\n{\n    // fall back: log, retarget to MediaElement, or disable media playback\n}","preventionTips":["Always target MediaTimelines at a named MediaElement","Never wrap the media target in a layout container when referencing it by name","Register names used by storyboards via RegisterName","Unit-test storyboard targets resolve to MediaElement before Begin"],"tags":["wpf","storyboard","animation","media"],"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"}