dotnet/wpf · error · ArgumentException

SR.SetterValueCannotBeUnset

Error message

SR.SetterValueCannotBeUnset

What it means

Thrown as ArgumentException from Setter.Initialize when the value passed to a Setter constructor is DependencyProperty.UnsetValue. A Setter must set an actual value; UnsetValue means 'no value' and is rejected rather than silently ignored.

Solutions

  1. Pass a real value (or use a different mechanism such as removing the Setter)
  2. Skip creating the Setter when the source value is DependencyProperty.UnsetValue
  3. Guard constructor inputs before building the style

Example fix

// before
object v = target.GetValue(prop);
style.Setters.Add(new Setter(prop, v)); // v may be UnsetValue
// after
object v = target.GetValue(prop);
if (v != DependencyProperty.UnsetValue)
    style.Setters.Add(new Setter(prop, v));
Defensive patterns

Strategy: validation

Validate before calling

if (value == DependencyProperty.UnsetValue) return; // skip instead of constructing Setter

Type guard

bool IsValidSetterValue(object v) => v != null && v != DependencyProperty.UnsetValue;

Try / catch

try { var s = new Setter(prop, value); } catch (ArgumentException) { /* value was UnsetValue */ }

Prevention

When it happens

Trigger: new Setter(prop, DependencyProperty.UnsetValue), typically when value came from a binding/GetValue result that returned Unset, or code that clears a setter by assigning UnsetValue.

Common situations: Programmatically building styles from property values read via GetValue (which can return UnsetValue); refactoring code that used UnsetValue as a 'clear' sentinel.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Setter.cs:49

            Initialize( property, value, null );
        }

        /// <summary>
        ///     Property Setter construction - given property, value, and string identifier for child node.
        /// </summary>
        public Setter( DependencyProperty property, object value, string targetName )
        {
            Initialize( property, value, targetName );
        }

        /// <summary>
        ///     Method that does all the initialization work for the constructors.
        /// </summary>
        private void Initialize( DependencyProperty property, object value, string target )
        {
            if( value == DependencyProperty.UnsetValue )
            {
                throw new ArgumentException(SR.SetterValueCannotBeUnset);
            }

            CheckValidProperty(property);

            // No null check for target since null is a valid value.

            _property = property;
            _value = value;
            _target = target;
        }

        private void CheckValidProperty( DependencyProperty property)
        {
            ArgumentNullException.ThrowIfNull(property);
            if (property.ReadOnly)
            {
                // Read-only properties will not be consulting Style/Template/Trigger Setter for value.
                //  Rather than silently do nothing, throw error.

View on GitHub (pinned to 81131a70a4)