{"record":{"id":"c7056ee2558f33f2","repo":"dotnet/wpf","slug":"sr-bindinggroup-cannotchangegroups","errorCode":null,"errorMessage":"SR.BindingGroup_CannotChangeGroups","messagePattern":"SR\\.BindingGroup_CannotChangeGroups","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/BindingExpressionBase.cs","lineNumber":1836,"sourceCode":"                if (explicitJoin)\n                {\n                    root.UpdateBindingGroup(bg);\n\n                    if (bg.SharesProposedValues && TraceData.IsEnabled)\n                    {\n                        TraceData.TraceAndNotifyWithNoParameters(TraceEventType.Warning,\n                                TraceData.SharesProposedValuesRequriesImplicitBindingGroup(\n                                        TraceData.Identify(root),\n                                        root.ParentBindingBase.BindingGroupName,\n                                        TraceData.Identify(bg)),\n                                this);\n                    }\n                }\n            }\n            else\n            {\n                if (root.BindingGroup != bg)\n                    throw new InvalidOperationException(SR.BindingGroup_CannotChangeGroups);\n            }\n        }\n\n        // mark a binding as non-grouped, so that we avoid doing the discovery again\n        private void MarkAsNonGrouped()\n        {\n            // Leaf bindings only get asked once, so there's no need to add a mark\n            if (!(this is BindingExpression))\n            {\n                SetValue(Feature.BindingGroup, NullBindingGroupReference);\n            }\n        }\n\n        // add to, or remove from, the CommitManager's set of dirty/invalid bindings\n        internal void NotifyCommitManager()\n        {\n            if (IsReflective && !IsDetached && !Engine.IsShutDown)\n            {","sourceCodeStart":1818,"sourceCodeEnd":1854,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/BindingExpressionBase.cs#L1818-L1854","documentation":"During BindingGroup discovery, a binding expression determines which BindingGroup it belongs to by walking up to the root item. If the root element's BindingGroup differs from the BindingGroup the expression already joined (bg), the state is inconsistent and BindingExpressionBase throws this InvalidOperationException. WPF considers re-parenting an expression into a different group an unsupported operation.","triggerScenarios":"Calling UpdateSource/UpdateTarget or adding a binding while the element tree is being re-parented so that the root's BindingGroup changes after the binding was grouped; programmatically reassigning a BindingGroup on a container while its bindings are already registered to a different group.","commonSituations":"Dynamic UI where BindingGroup is swapped or the element is moved between containers with different BindingGroups; DataTemplates reused across items with differing group state; manual code that sets BindingGroup after bindings have activated.","solutions":["Set the BindingGroup on the container/root element BEFORE bindings are established (e.g. in XAML or before children load).","Do not change or move a BindingGroup while its bindings are active; create a fresh BindingGroup and re-apply bindings.","If re-parenting elements, ensure the destination root's BindingGroup matches the original group the bindings joined."],"exampleFix":"// before: group swapped after bindings activated\ngrid.BindingGroup = newBindingGroup; // throws for existing bindings\n\n// after: apply group first, then re-create bindings\ngrid.BindingGroup = newBindingGroup;\ngrid.SetBinding(TextBox.TextProperty, new Binding(\"Value\"));","handlingStrategy":"validation","validationCode":"var rootGroup = rootElement.BindingGroup;\nif (rootGroup != null && expression.BindingGroup != null && expression.BindingGroup != rootGroup)\n    throw new InvalidOperationException(\"Binding already belongs to a different BindingGroup\");","typeGuard":null,"tryCatchPattern":"try { container.BindingGroup = newGroup; }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"group\"))\n{\n    // rebuild bindings under the new group\n}","preventionTips":["Assign BindingGroup before bindings activate","Never move elements between containers with different BindingGroups while bound","Re-create bindings after changing a group"],"tags":["wpf","data-binding","binding-group","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"}