{"record":{"id":"7744d394dd0a0e7a","repo":"dotnet/wpf","slug":"sr-format-sr-bindingconflict-sourceproperties-source","errorCode":null,"errorMessage":"SR.Format(SR.BindingConflict, SourceProperties.Source, _sourceInUse)","messagePattern":"SR\\.Format\\(SR\\.BindingConflict, SourceProperties\\.Source, _sourceInUse\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/Binding.cs","lineNumber":516,"sourceCode":"            set\n            {\n                CheckSealed();\n\n                if (_sourceInUse == SourceProperties.None || _sourceInUse == SourceProperties.Source)\n                {\n                    if (value != DependencyProperty.UnsetValue)\n                    {\n                        SetValue(Feature.ObjectSource, new WeakReference<object>(value));\n                        SourceReference = new ExplicitObjectRef(value);\n                    }\n                    else\n                    {\n                        ClearValue(Feature.ObjectSource);\n                        SourceReference = null;\n                    }\n                }\n                else\n                    throw new InvalidOperationException(SR.Format(SR.BindingConflict, SourceProperties.Source, _sourceInUse));\n            }\n        }\n\n        /// <summary>\n        /// This method is used by TypeDescriptor to determine if this property should\n        /// be serialized.\n        /// </summary>\n        [EditorBrowsable(EditorBrowsableState.Never)]\n        public bool ShouldSerializeSource()\n        {\n            //return _objectSource.IsAlive && _objectSource.Target != DependencyProperty.UnsetValue;\n\n            // M8.2: always false\n            return false;\n        }\n\n        /// <summary>\n        /// Description of the object to use as the source, relative to the target element.","sourceCodeStart":498,"sourceCodeEnd":534,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/Binding.cs#L498-L534","documentation":"Setting Binding.Source when another source (RelativeSource or ElementName) is already in use throws InvalidOperationException with SR.BindingConflict. Only one source mechanism is allowed per Binding; the setter checks _sourceInUse before accepting the new value.","triggerScenarios":"binding.Source = obj on a binding that already has ElementName or RelativeSource set; XAML that declares both Source and ElementName on one Binding; programmatically merging two binding definitions.","commonSituations":"Cloning templates that preset RelativeSource and then adding a Source; refactoring XAML where one source attribute was left behind; sharing a Binding instance across controls.","solutions":["Remove the competing source (ElementName=\"\"/RelativeSource=null) before setting Source, or declare only one in XAML.","Create a new Binding for the alternate source instead of mutating the existing one.","Centralize binding construction in a helper that asserts a single source is set."],"exampleFix":"// before\n<Binding Path=\"Text\" ElementName=\"tb\" Source=\"{StaticResource vm}\" />\n// after\n<Binding Path=\"Text\" Source=\"{StaticResource vm}\" />","handlingStrategy":"validation","validationCode":"static bool CanSetSource(Binding b) =>\n    b.RelativeSource == null && string.IsNullOrEmpty(b.ElementName);","typeGuard":null,"tryCatchPattern":"try { binding.Source = vm; } catch (InvalidOperationException) { binding = new Binding(path) { Source = vm }; }","preventionTips":["In XAML, never combine Source with ElementName or RelativeSource on one Binding.","When overriding a template binding, create a new Binding rather than mutating.","Add a code-review rule that each Binding sets exactly one source."],"tags":["wpf","binding","data-binding","conflict"],"backgroundTag":"conflicting-config-options","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"}