{"record":{"id":"f115f8ad1925c4b3","repo":"dotnet/wpf","slug":"sr-format-sr-bindingexpressionstatuschanged-status-status","errorCode":null,"errorMessage":"SR.Format(SR.BindingExpressionStatusChanged, _status, status)","messagePattern":"SR\\.Format\\(SR\\.BindingExpressionStatusChanged, _status, status\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/BindingExpressionBase.cs","lineNumber":2106,"sourceCode":"            {\n                // set IEnumerable properties (e.g. ItemsSource) to null, so that\n                // the control can disconnect from the individual items\n                newValue = null;\n            }\n\n            // notify the target property about the new value (cheaply)\n            if (newValue != DependencyProperty.UnsetValue)\n            {\n                ChangeValue(newValue, false);\n                Invalidate(false);\n            }\n        }\n\n        internal void SetStatus(BindingStatusInternal status)\n        {\n            if (IsDetached && status != _status)\n            {\n                throw new InvalidOperationException(SR.Format(SR.BindingExpressionStatusChanged, _status, status));\n            }\n\n            _status = status;\n        }\n\n        // convert a user-supplied fallback value to a usable equivalent\n        //  returns:    UnsetValue          if user did not supply a fallback value\n        //              value               if fallback value is legal\n        //              DefaultValueObject  otherwise\n        internal static object ConvertFallbackValue(object value, DependencyProperty dp, object sender)\n        {\n            Exception e;\n            object result = ConvertValue(value, dp, out e);\n\n            if (result == DefaultValueObject)\n            {\n                if (TraceData.IsEnabled)\n                {","sourceCodeStart":2088,"sourceCodeEnd":2124,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/BindingExpressionBase.cs#L2088-L2124","documentation":"SetStatus is the single mutation point for a binding expression's internal status. Once an expression is Detached (removed from its binding target), its status is frozen; attempting to transition to any different status throws this InvalidOperationException, preventing post-detach state corruption. The message includes the current status and the requested one.","triggerScenarios":"Calling SetStatus with a status different from the current one on an expression whose IsDetached is true — typically from reactivating, transferring, or updating a BindingExpressionBase after Detach (e.g. reusing a BindingExpression whose target property or element was cleared).","commonSituations":"Reusing binding expression objects across element re-templating; updating a binding after the element was removed from the visual tree; custom code or third-party frameworks that poke at binding internals during unload.","solutions":["Do not reuse BindingExpression objects after detach; create a new Binding and call SetBinding again.","Ensure UpdateSource/UpdateTarget calls only happen while the binding is attached (check expression.IsDetached first).","If holding references to BindingExpressionBase, null them out on Unloaded/Detached events instead of reusing them."],"exampleFix":"// before\nvar expr = (BindingExpression)textBox.GetBindingExpression(TextBox.TextProperty);\n// ... element detached ...\nexpr.UpdateSource(); // throws\n\n// after\nvar expr = (BindingExpression)textBox.GetBindingExpression(TextBox.TextProperty);\nif (expr != null && !expr.IsDetached) expr.UpdateSource();","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"bool CanUpdate(BindingExpressionBase expr) => expr != null && !expr.IsDetached;","tryCatchPattern":"try { expr.UpdateSource(); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"status\"))\n{\n    // expression detached; recreate binding\n}","preventionTips":["Check IsDetached before reusing a BindingExpression","Drop cached expression references on Unloaded","Create fresh bindings rather than reactivating detached ones"],"tags":["wpf","data-binding","lifecycle","invalid-state"],"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-21T21:30:21.729Z"}