dotnet/wpf · error · ArgumentException

SR.MustBeCondition

Error message

SR.MustBeCondition

What it means

ArgumentException from ConditionCollection.ConditionValidation (InsertItem/SetItem guard): the object being added to the collection is not a Condition, which is the only element type the strongly-typed collection accepts.

Solutions

  1. Add only Condition instances to ConditionCollection
  2. Wrap foreign values in an appropriate Condition before insertion

Example fix

// before
conditions.Add(someTrigger); // ArgumentException
// after
conditions.Add(new Condition(UIElement.IsEnabledProperty, true));
Defensive patterns

Strategy: type-guard

Validate before calling

if (item is not Condition)
    throw new ArgumentException("Item must be a Condition", nameof(item));

Type guard

bool IsCondition(object o) => o is Condition;

Try / catch

try { conditions.Add(item); }
catch (ArgumentException ex) { /* convert item to a Condition first */ }

Prevention

When it happens

Trigger: Calling Add/Insert or the indexer setter on a ConditionCollection with an object that is not a Condition (e.g. raw Trigger, string, or null — null also fails earlier).

Common situations: Non-generic collection misuse in older code paths; reflection or data-binding pushing arbitrary items into the collection; type confusion between Trigger and Condition collections.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/ConditionCollection.cs:112

        
        #region PrivateMethods

        private void CheckSealed()
        {
            if (_sealed)
            {
                throw new InvalidOperationException(SR.Format(SR.CannotChangeAfterSealed, "ConditionCollection"));
            }
        }
        
        private void ConditionValidation(object value)
        {
            ArgumentNullException.ThrowIfNull(value);

            Condition condition = value as Condition;
            if (condition == null)
            {
                throw new ArgumentException(SR.MustBeCondition);
            }
        }

        #endregion PrivateMethods

        #region Data
    
        private bool _sealed;

        #endregion Data
    }
}


View on GitHub (pinned to 81131a70a4)