dotnet/wpf · error · InvalidOperationException

SR.Format(SR.BindingConflict…

Error message

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

What it means

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.

Solutions

  1. Clear the existing source before assigning RelativeSource, or keep only RelativeSource in the declaration.
  2. Construct a new Binding when you need different source semantics.
  3. In XAML use RelativeSource={RelativeSource ...} alone, or ElementName alone — never both.

Example fix

// before
<Binding Path="Text" ElementName="tb" RelativeSource="{RelativeSource Self}" />
// after
<Binding Path="Text" ElementName="tb" />
Defensive patterns

Strategy: validation

Validate before calling

static bool CanSetRelativeSource(Binding b) =>
    b.Source == null && string.IsNullOrEmpty(b.ElementName);

Try / catch

try { binding.RelativeSource = relSrc; } catch (InvalidOperationException) { binding = new Binding(path) { RelativeSource = relSrc }; }

Prevention

When it happens

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

Common situations: Template refactors where a RelativeSource was added without removing ElementName; code that programmatically enhances existing bindings from DataTemplates; copy-pasted XAML attributes.

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/c3ad7642236887b0. Report an issue: GitHub.

Appendix: source

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

        /// <summary>
        /// Description of the object to use as the source, relative to the target element.
        /// </summary>
        [DefaultValue(null)]
        public RelativeSource RelativeSource
        {
            get { return (RelativeSource)GetValue(Feature.RelativeSource, null); }
            set
            {
                CheckSealed();

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

        /// <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

View on GitHub (pinned to 81131a70a4)