dotnet/wpf · error · InvalidOperationException

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

Error message

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

What it means

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.

Solutions

  1. Remove the competing source (ElementName=""/RelativeSource=null) before setting Source, or declare only one in XAML.
  2. Create a new Binding for the alternate source instead of mutating the existing one.
  3. Centralize binding construction in a helper that asserts a single source is set.

Example fix

// before
<Binding Path="Text" ElementName="tb" Source="{StaticResource vm}" />
// after
<Binding Path="Text" Source="{StaticResource vm}" />
Defensive patterns

Strategy: validation

Validate before calling

static bool CanSetSource(Binding b) =>
    b.RelativeSource == null && string.IsNullOrEmpty(b.ElementName);

Try / catch

try { binding.Source = vm; } catch (InvalidOperationException) { binding = new Binding(path) { Source = vm }; }

Prevention

When it happens

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

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

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

Appendix: source

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

            set
            {
                CheckSealed();

                if (_sourceInUse == SourceProperties.None || _sourceInUse == SourceProperties.Source)
                {
                    if (value != DependencyProperty.UnsetValue)
                    {
                        SetValue(Feature.ObjectSource, new WeakReference<object>(value));
                        SourceReference = new ExplicitObjectRef(value);
                    }
                    else
                    {
                        ClearValue(Feature.ObjectSource);
                        SourceReference = null;
                    }
                }
                else
                    throw new InvalidOperationException(SR.Format(SR.BindingConflict, SourceProperties.Source, _sourceInUse));
            }
        }

        /// <summary>
        /// This method is used by TypeDescriptor to determine if this property should
        /// be serialized.
        /// </summary>
        [EditorBrowsable(EditorBrowsableState.Never)]
        public bool ShouldSerializeSource()
        {
            //return _objectSource.IsAlive && _objectSource.Target != DependencyProperty.UnsetValue;

            // M8.2: always false
            return false;
        }

        /// <summary>
        /// Description of the object to use as the source, relative to the target element.

View on GitHub (pinned to 81131a70a4)