dotnet/wpf · error · InvalidOperationException

SR.Format(SR.BindingConflict, SourceProperties.ElementName…

Error message

SR.Format(SR.BindingConflict, SourceProperties.ElementName, _sourceInUse)

What it means

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.

Solutions

  1. Use only one source mechanism per Binding: set ElementName, RelativeSource, or Source exclusively
  2. Create a fresh Binding instance instead of mutating an existing one whose source mode is already chosen
  3. Check your configuration path: if ElementName is needed, remove/avoid Source, RelativeSource, and XPath assignments on the same binding

Example fix

// before
var b = new Binding("Text") { Source = someObject };
b.ElementName = "OtherElement"; // throws InvalidOperationException
// after
var b = new Binding("Text") { ElementName = "OtherElement" };
Defensive patterns

Strategy: validation

Validate before calling

if (b.Source != null || b.RelativeSource != null && wantElementName) throw new InvalidOperationException("Binding already has a source; cannot also set ElementName");

Type guard

static bool CanSetElementName(Binding b) => b.Source == null && b.RelativeSource == null;

Try / catch

try { b.ElementName = name; } catch (InvalidOperationException ex) when (ex.Message.Contains("conflict") || ex.Message.Contains("ElementName")) { /* rebuild binding with single source mode */ }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: Conflicting config options: "cannot be used together" — configuration validation errors across open-source libraries — this error's family across 162 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/2a7631bf585ce3b5. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Data/Binding.cs:569

            }
        }

        /// <summary> Name of the element to use as the source </summary>
        [DefaultValue(null)]
        public string ElementName
        {
            get { return (string)GetValue(Feature.ElementSource, null); }
            set
            {
                CheckSealed();

                if (_sourceInUse == SourceProperties.None || _sourceInUse == SourceProperties.ElementName)
                {
                    SetValue(Feature.ElementSource, value, null);
                    SourceReference = (value != null) ? new ElementObjectRef(value) : null;
                }
                else
                    throw new InvalidOperationException(SR.Format(SR.BindingConflict, SourceProperties.ElementName, _sourceInUse));
            }
        }

        /// <summary> True if Binding should get/set values asynchronously </summary>
        [DefaultValue(false)]
        public bool IsAsync
        {
            get { return _isAsync; }
            set { CheckSealed();  _isAsync = value; }
        }

        /// <summary> Opaque data passed to the asynchronous data dispatcher </summary>
        [DefaultValue(null)]
        public object AsyncState
        {
            get { return GetValue(Feature.AsyncState, null); }
            set { CheckSealed(); SetValue(Feature.AsyncState, value, null); }
        }

View on GitHub (pinned to 81131a70a4)