{"record":{"id":"c3ad7642236887b0","repo":"dotnet/wpf","slug":"sr-format-sr-bindingconflict-sourceproperties-relativesource","errorCode":null,"errorMessage":"SR.Format(SR.BindingConflict, SourceProperties.RelativeSource, _sourceInUse)","messagePattern":"SR\\.Format\\(SR\\.BindingConflict, SourceProperties\\.RelativeSource, _sourceInUse\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/Binding.cs","lineNumber":550,"sourceCode":"\n        /// <summary>\n        /// Description of the object to use as the source, relative to the target element.\n        /// </summary>\n        [DefaultValue(null)]\n        public RelativeSource RelativeSource\n        {\n            get { return (RelativeSource)GetValue(Feature.RelativeSource, null); }\n            set\n            {\n                CheckSealed();\n\n                if (_sourceInUse == SourceProperties.None || _sourceInUse == SourceProperties.RelativeSource)\n                {\n                    SetValue(Feature.RelativeSource, value, null);\n                    SourceReference = (value != null) ? new RelativeObjectRef(value) : null;\n                }\n                else\n                    throw new InvalidOperationException(SR.Format(SR.BindingConflict, SourceProperties.RelativeSource, _sourceInUse));\n            }\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","sourceCodeStart":532,"sourceCodeEnd":568,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/Binding.cs#L532-L568","documentation":"Setting Binding.RelativeSource when _sourceInUse already indicates another source (Source, ElementName, or StaticSource) throws InvalidOperationException with SR.BindingConflict naming RelativeSource and the current source. A Binding accepts exactly one source mechanism.","triggerScenarios":"binding.RelativeSource = new RelativeSource(RelativeSourceMode.Self) on a binding with Source or ElementName already set; XAML combining RelativeSource with ElementName/Source on the same Binding.","commonSituations":"Template refactors where a RelativeSource was added without removing ElementName; code that programmatically enhances existing bindings from DataTemplates; copy-pasted XAML attributes.","solutions":["Clear the existing source before assigning RelativeSource, or keep only RelativeSource in the declaration.","Construct a new Binding when you need different source semantics.","In XAML use RelativeSource={RelativeSource ...} alone, or ElementName alone — never both."],"exampleFix":"// before\n<Binding Path=\"Text\" ElementName=\"tb\" RelativeSource=\"{RelativeSource Self}\" />\n// after\n<Binding Path=\"Text\" ElementName=\"tb\" />","handlingStrategy":"validation","validationCode":"static bool CanSetRelativeSource(Binding b) =>\n    b.Source == null && string.IsNullOrEmpty(b.ElementName);","typeGuard":null,"tryCatchPattern":"try { binding.RelativeSource = relSrc; } catch (InvalidOperationException) { binding = new Binding(path) { RelativeSource = relSrc }; }","preventionTips":["Never declare both RelativeSource and ElementName/Source in one Binding element.","When deriving bindings from templates, strip the original source before adding RelativeSource.","Centralize Binding creation in a factory that enforces single-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"}