dotnet/wpf · error · InvalidOperationException

SR.ConditionCannotUseBothPropertyAndBinding

Error message

SR.ConditionCannotUseBothPropertyAndBinding

What it means

InvalidOperationException from Condition.Property setter: the Condition was already configured with a Binding, and a Condition cannot specify both Property and Binding — the setter guards the mutual exclusion between trigger-property and data-binding conditions.

Solutions

  1. Use either Property or Binding in a Condition, not both
  2. Create a separate Condition for the property-based check

Example fix

// before
condition.Binding = binding;
condition.Property = MyProp; // throws
// after
var cond1 = new Condition { Binding = binding };
var cond2 = new Condition(MyProp, value);
Defensive patterns

Strategy: validation

Validate before calling

if (condition.Binding != null) throw new InvalidOperationException("Condition already has a Binding; create a new Condition for Property-based checks");

Try / catch

try { condition.Property = p; } catch (InvalidOperationException ex) when (ex.Message.Contains("Property") || ex.Message.Contains("Binding")) { /* use separate conditions */ }

Prevention

When it happens

Trigger: Assigning Condition.Property after Condition.Binding was already assigned on the same Condition instance.

Common situations: Reconfiguring a Condition from a binding-based to property-based check without clearing Binding first; building MultiTrigger conditions in a loop that sets both.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Condition.cs:88

        /// <summary>
        ///     DepedencyProperty of the conditional
        /// </summary>
        [Ambient]
        [DefaultValue(null)]
        public DependencyProperty Property
        {
            get { return _property; }
            set
            {
                if (_sealed)
                {
                    throw new InvalidOperationException(SR.Format(SR.CannotChangeAfterSealed, "Condition"));
                }

                if (_binding != null)
                {
                    throw new InvalidOperationException(SR.ConditionCannotUseBothPropertyAndBinding);
                }

                _property = value;
            }
        }

        /// <summary>
        ///     Binding of the conditional
        /// </summary>
        [DefaultValue(null)]
        public BindingBase Binding
        {
            get { return _binding; }
            set
            {
                if (_sealed)
                {
                    throw new InvalidOperationException(SR.Format(SR.CannotChangeAfterSealed, "Condition"));

View on GitHub (pinned to 81131a70a4)