{"record":{"id":"e0266cbc134f0d5f","repo":"dotnet/wpf","slug":"sr-shareableexpressionscannotchangesources","errorCode":null,"errorMessage":"SR.ShareableExpressionsCannotChangeSources","messagePattern":"SR\\.ShareableExpressionsCannotChangeSources","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/Expression.cs","lineNumber":207,"sourceCode":"        /// </summary>\n        /// <remarks>\n        ///     Expression must be in use on provided DependencyObject/DependencyProperty.\n        ///     GetSources must reflect the old sources to be replaced by the provided newSources.\n        /// </remarks>\n        /// <param name=\"d\">DependencyObject whose sources are to be updated</param>\n        /// <param name=\"dp\">The property that the Expression is set to</param>\n        /// <param name=\"newSources\">New sources</param>\n        internal void ChangeSources(DependencyObject d, DependencyProperty dp, DependencySource[] newSources)\n        {\n            if (!ForwardsInvalidations)\n            {\n                ArgumentNullException.ThrowIfNull(d);\n                ArgumentNullException.ThrowIfNull(dp);\n            }\n\n            if (Shareable)\n            {\n                throw new InvalidOperationException(SR.ShareableExpressionsCannotChangeSources);\n            }\n\n            DependencyObject.ValidateSources(d, newSources, this);\n\n            // Additional validation in callee\n            if (ForwardsInvalidations)\n            {\n                DependencyObject.ChangeExpressionSources(this, null, null, newSources);\n            }\n            else\n            {\n                DependencyObject.ChangeExpressionSources(this, d, dp, newSources);\n            }\n        }\n\n\n        // Determines if Expression can be attached:\n        //    1) If Shareable","sourceCodeStart":189,"sourceCodeEnd":225,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/Expression.cs#L189-L225","documentation":"Expression.ChangeSources updates the list of DependencyObjects the expression depends on, but a shareable expression (e.g. a shared StaticResource-style expression) is immutable with respect to its sources. If the expression is marked Shareable, ChangeSources throws InvalidOperationException(SR.ShareableExpressionsCannotChangeSources) because mutating shared expressions would affect every consumer.","triggerScenarios":"Calling ChangeSources (directly or via DependencyObject expression invalidation plumbing) on an expression constructed with ExpressionMode.Shareable after it has been attached and used.","commonSituations":"Custom markup extensions or expression subclasses that start sharing instances (shareable mode) but then attempt dynamic source changes; reusing one expression instance across multiple dependency properties while its sources evolve.","solutions":["Construct the expression without the Shareable ExpressionMode if its sources need to change over time","Create a new expression instance per target instead of sharing one instance","Re-evaluate/replace the whole expression (e.g. re-apply the markup extension) rather than mutating sources","Split logic: use a shareable immutable expression for static cases and a non-shareable one for dynamic cases"],"exampleFix":"// before\nvar expr = new ObjectRefExpression(obj, prop, ExpressionMode.Shareable);\nexpr.ChangeSources(target, newSources); // throws\n// after\nvar expr = new ObjectRefExpression(obj, prop, ExpressionMode.None);\nexpr.ChangeSources(target, newSources); // ok","handlingStrategy":"type-guard","validationCode":"if (expression is { Shareable: true }) throw new NotSupportedException(\"Cannot change sources of a shareable expression\");","typeGuard":"static bool CanChangeSources(System.Windows.Expression e) => !e.Shareable;","tryCatchPattern":"try { expr.ChangeSources(d, newSources); }\ncatch (InvalidOperationException ex) when (expr.Shareable) { log.Warn(\"Shareable expression is immutable; recreating\", ex); }","preventionTips":["Use Shareable mode only for expressions whose dependencies never change","Create one expression instance per target when sources are dynamic","Replace whole expressions instead of mutating shared ones"],"tags":["wpf","binding","shareable-expression","invalid-state"],"backgroundTag":"unsupported-operation","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"}