{"record":{"id":"78168a0fbd010121","repo":"dotnet/wpf","slug":"sr-bindingexpressionisdetached","errorCode":null,"errorMessage":"SR.BindingExpressionIsDetached","messagePattern":"SR\\.BindingExpressionIsDetached","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/BindingExpression.cs","lineNumber":183,"sourceCode":"\n                // otherwise use the explicit source\n                ObjectRef or = ParentBinding.SourceReference;\n                return or.GetObject(target, new ObjectRefArgs());\n            }\n        }\n\n        //------------------------------------------------------\n        //\n        //  Public Methods\n        //\n        //------------------------------------------------------\n\n        /// <summary> Send the current value back to the source </summary>\n        /// <remarks> Does nothing when binding's Mode is not TwoWay or OneWayToSource </remarks>\n        public override void UpdateSource()\n        {\n            if (IsDetached)\n                throw new InvalidOperationException(SR.BindingExpressionIsDetached);\n\n            NeedsUpdate = true;     // force update\n            Update();               // update synchronously\n        }\n\n        /// <summary> Force a data transfer from source to target </summary>\n        public override void UpdateTarget()\n        {\n            if (IsDetached)\n                throw new InvalidOperationException(SR.BindingExpressionIsDetached);\n\n            Worker?.RefreshValue();  // calls TransferValue\n        }\n\n#region Expression overrides\n\n        /// <summary>\n        ///     Notification that a Dependent that this Expression established has","sourceCodeStart":165,"sourceCodeEnd":201,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/BindingExpression.cs#L165-L201","documentation":"BindingExpression.UpdateSource throws this InvalidOperationException when the binding expression is detached (IsDetached), i.e. no longer connected to its target/source — usually because the target element was unloaded, the binding was replaced, or the target was removed from the visual tree. A detached expression cannot push values back to the source.","triggerScenarios":"Calling BindingExpression.UpdateSource() explicitly on an expression obtained from GetBindingExpression after the target was detached (element unloaded from visual tree, binding replaced, window closed, or ItemsContainer recycled).","commonSituations":"Saving data on window close by calling UpdateSource on cached BindingExpression references; calling UpdateSource in an Unloaded handler; holding expressions for items removed from an ItemsControl.","solutions":["Verify IsDetached before calling UpdateSource and skip or re-obtain the expression","Re-acquire the expression with target.GetBindingExpression(dp) just before use, instead of caching it","Move the update logic to before detachment (e.g. Updating/commit on Closing of the source element rather than Unloaded)"],"exampleFix":"// before\nexpr.UpdateSource(); // throws if detached\n// after\nif (!expr.IsDetached) expr.UpdateSource();","handlingStrategy":"type-guard","validationCode":"if (expr.IsDetached) return; // skip or re-acquire expression","typeGuard":"static bool CanUpdate(BindingExpression e) => e != null && !e.IsDetached;","tryCatchPattern":"try { expr.UpdateSource(); } catch (InvalidOperationException ex) when (ex.Message.Contains(\"detached\")) { expr = target.GetBindingExpression(targetProperty); if (expr?.IsDetached == false) expr.UpdateSource(); }","preventionTips":["Re-acquire BindingExpression via GetBindingExpression right before use; avoid caching","Avoid calling UpdateSource from Unloaded handlers or after element removal","Check IsDetached defensively in save/commit code paths"],"tags":["wpf","data-binding","detached-binding","invalid-operation"],"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"}