{"record":{"id":"2a7631bf585ce3b5","repo":"dotnet/wpf","slug":"sr-format-sr-bindingconflict-sourceproperties-elementname","errorCode":null,"errorMessage":"SR.Format(SR.BindingConflict, SourceProperties.ElementName, _sourceInUse)","messagePattern":"SR\\.Format\\(SR\\.BindingConflict, SourceProperties\\.ElementName, _sourceInUse\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/Binding.cs","lineNumber":569,"sourceCode":"            }\n        }\n\n        /// <summary> Name of the element to use as the source </summary>\n        [DefaultValue(null)]\n        public string ElementName\n        {\n            get { return (string)GetValue(Feature.ElementSource, null); }\n            set\n            {\n                CheckSealed();\n\n                if (_sourceInUse == SourceProperties.None || _sourceInUse == SourceProperties.ElementName)\n                {\n                    SetValue(Feature.ElementSource, value, null);\n                    SourceReference = (value != null) ? new ElementObjectRef(value) : null;\n                }\n                else\n                    throw new InvalidOperationException(SR.Format(SR.BindingConflict, SourceProperties.ElementName, _sourceInUse));\n            }\n        }\n\n        /// <summary> True if Binding should get/set values asynchronously </summary>\n        [DefaultValue(false)]\n        public bool IsAsync\n        {\n            get { return _isAsync; }\n            set { CheckSealed();  _isAsync = value; }\n        }\n\n        /// <summary> Opaque data passed to the asynchronous data dispatcher </summary>\n        [DefaultValue(null)]\n        public object AsyncState\n        {\n            get { return GetValue(Feature.AsyncState, null); }\n            set { CheckSealed(); SetValue(Feature.AsyncState, value, null); }\n        }","sourceCodeStart":551,"sourceCodeEnd":587,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/Binding.cs#L551-L587","documentation":"This InvalidOperationException is thrown by Binding.Source's setter when the binding already committed to a different source mechanism (RelativeSource, Source, or XPath) and you then try to set ElementName. A WPF Binding may use only one source mode; _sourceInUse tracks which was chosen first, so a second conflicting assignment is rejected. It protects against ambiguous data-source configuration.","triggerScenarios":"Setting binding.ElementName after RelativeSource, Source, or an XPath-based source was already set on the same Binding instance — typically by mutating a binding in code after partially configuring it, or assigning conflicting properties in any order.","commonSituations":"Building bindings programmatically (e.g. in a behavior or helper) where ElementName is set conditionally after Source/RelativeSource was already assigned; reusing one Binding object for several targets and changing its source mode; XAML converters/helpers constructing bindings from mixed inputs.","solutions":["Use only one source mechanism per Binding: set ElementName, RelativeSource, or Source exclusively","Create a fresh Binding instance instead of mutating an existing one whose source mode is already chosen","Check your configuration path: if ElementName is needed, remove/avoid Source, RelativeSource, and XPath assignments on the same binding"],"exampleFix":"// before\nvar b = new Binding(\"Text\") { Source = someObject };\nb.ElementName = \"OtherElement\"; // throws InvalidOperationException\n// after\nvar b = new Binding(\"Text\") { ElementName = \"OtherElement\" };","handlingStrategy":"validation","validationCode":"if (b.Source != null || b.RelativeSource != null && wantElementName) throw new InvalidOperationException(\"Binding already has a source; cannot also set ElementName\");","typeGuard":"static bool CanSetElementName(Binding b) => b.Source == null && b.RelativeSource == null;","tryCatchPattern":"try { b.ElementName = name; } catch (InvalidOperationException ex) when (ex.Message.Contains(\"conflict\") || ex.Message.Contains(\"ElementName\")) { /* rebuild binding with single source mode */ }","preventionTips":["Assign exactly one of ElementName, Source, RelativeSource per binding","Build bindings in a single configuration pass, never mutate after use","Encapsulate binding creation in factory helpers that enforce one-source mode"],"tags":["wpf","data-binding","binding-conflict","invalid-operation"],"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"}