{"record":{"id":"6ae76361b93582b5","repo":"dotnet/wpf","slug":"sr-mediaelement-cannotsetsourceonmediaelementdrivenbyclock","errorCode":null,"errorMessage":"SR.MediaElement_CannotSetSourceOnMediaElementDrivenByClock","messagePattern":"SR\\.MediaElement_CannotSetSourceOnMediaElementDrivenByClock","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/AVElementHelper.cs","lineNumber":721,"sourceCode":"        /// <summary>\n        /// Raised when source is changed\n        /// </summary>\n        internal static void OnSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)\n        {\n            if (e.IsASubPropertyChange)\n            {\n                return;\n            }\n\n            AVElementHelper aveh = AVElementHelper.GetHelper(d);\n            aveh.MemberOnInvalidateSource(e); // call non-static\n        }\n\n        private void MemberOnInvalidateSource(DependencyPropertyChangedEventArgs e)\n        {\n            if (_clock._value != null)\n            {\n                throw new InvalidOperationException(SR.MediaElement_CannotSetSourceOnMediaElementDrivenByClock);\n            }\n\n            _source._value = (Uri)e.NewValue;\n            _source._wasSet = _source._isSet = true;\n\n            HandleStateChange();\n        }\n\n        /// <summary>\n        /// Raised when there is a error with media playback\n        /// </summary>\n        private\n        void\n        OnMediaFailed(\n            object sender,\n            ExceptionEventArgs args\n            )\n        {","sourceCodeStart":703,"sourceCodeEnd":739,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/AVElementHelper.cs#L703-L739","documentation":"AVElementHelper.MemberOnInvalidateSource throws this InvalidOperationException when the Source property of a MediaElement is changed while the element is driven by a MediaClock. A clock-driven MediaElement has its timeline owned by the clock, so setting Source directly is not allowed.","triggerScenarios":"Assigning mediaElement.Source (or a binding/style updating Source) on a MediaElement whose Clock was set via ClockController or MediaClock assignment.","commonSituations":"Mixing clock-based playback (Storyboard/MediaClock with ClockController.Seek/Play) with source updates; data-binding Source on a clock-controlled element; style triggers that change Source during clock playback.","solutions":["Stop using the clock (e.g. remove the MediaClock/ClockController) before changing Source, or create a new MediaElement.","Control playback exclusively through ClockController when using clocks, and set the source at clock creation time.","Guard Source updates with a check that the element is not clock-driven."],"exampleFix":"// before\nmedia.Clock = clock;\nmedia.Source = new Uri(\"other.wmv\"); // throws\n// after\nmedia.Clock = null; // detach clock first\nmedia.Source = new Uri(\"other.wmv\");","handlingStrategy":"try-catch","validationCode":"if (media.Clock != null) throw new InvalidOperationException(\"Detach the MediaClock before changing Source\");","typeGuard":"bool IsClockDriven(MediaElement m) => m.Clock != null;","tryCatchPattern":"try { media.Source = newUri; } catch (InvalidOperationException) { media.Clock = null; media.Source = newUri; }","preventionTips":["Do not mix ClockController-driven playback with Source property changes","Set the source when creating the clock; update via ClockController instead","Null out media.Clock before rebinding Source"],"tags":["wpf","mediaelement","invalid-operation","media-clock"],"backgroundTag":"invalid-state-transition","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}