dotnet/wpf · error · ArgumentException

SR.Format(SR.ReadOnlyPropertyNotAllowed, property.Name…

Error message

SR.Format(SR.ReadOnlyPropertyNotAllowed, property.Name, GetType().Name)

What it means

Thrown as ArgumentException from Setter.CheckValidProperty when the DependencyProperty passed to a Setter is read-only (DependencyProperty.ReadOnly == true). Read-only properties are set by the system, never via Style/Template/Trigger setters, so WPF refuses them explicitly.

Solutions

  1. Use a writable DependencyProperty instead (register your own, or style the underlying writable property)
  2. If the property comes from metadata, check property.ReadOnly before assigning Setter.Property
  3. For read-only DPs, manipulate their value via the owner's internal mechanisms, not setters

Example fix

// before
new Setter(UIElement.IsMouseOverProperty, true); // read-only
// after
// style a trigger instead
new Trigger { Property = UIElement.IsMouseOverProperty, Value = true, Setters = { new Setter(UIElement.OpacityProperty, 0.5) } }
Defensive patterns

Strategy: validation

Validate before calling

if (property != null && property.ReadOnly) throw new ArgumentException($"{property.Name} is read-only and cannot be set via a Setter");

Type guard

bool CanStyle(DependencyProperty p) => p != null && !p.ReadOnly;

Try / catch

try { var s = new Setter(property, value); } catch (ArgumentException) { /* property was read-only */ }

Prevention

When it happens

Trigger: new Setter(SomeClass.SomeReadOnlyProperty, value) or assigning such a property to Setter.Property — e.g. trying to style read-only properties like ActualWidth, IsMouseOver, or custom readonly DPs registered with RegisterReadOnly.

Common situations: Attempting to 'override' a read-only property in a style; confusion between a read-only DP and its read-only counterpart; trying to animate/style values driven by the system.

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

Appendix: source

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

            }

            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.
                throw new ArgumentException(SR.Format(SR.ReadOnlyPropertyNotAllowed, property.Name, GetType().Name));
            }
            if( property == FrameworkElement.NameProperty)
            {
                throw new InvalidOperationException(SR.Format(SR.CannotHavePropertyInStyle, FrameworkElement.NameProperty.Name));
            }
        }

        /// <summary>
        ///     Seals this setter
        /// </summary>
        internal override void Seal()
        {

            // Do the validation that can't be done until we know all of the property
            // values.

            DependencyProperty dp = Property;
            object value = ValueInternal;

View on GitHub (pinned to 81131a70a4)