dotnet/wpf · error · InvalidOperationException

SR.Format(SR.CannotHavePropertyInStyle…

Error message

SR.Format(SR.CannotHavePropertyInStyle, FrameworkElement.NameProperty.Name)

What it means

Thrown as InvalidOperationException from Setter.CheckValidProperty when the property is FrameworkElement.NameProperty. The element Name participates in namescopes and cannot be changed by a Style/Template/Trigger setter, so WPF forbids it outright.

Solutions

  1. Remove the Setter targeting NameProperty
  2. Set the name at element construction/XAML declaration (x:Name) instead
  3. If conditional naming is needed, create distinct elements or use Tag/attached properties for state

Example fix

// before
style.Setters.Add(new Setter(FrameworkElement.NameProperty, "Renamed"));
// after
style.Setters.Add(new Setter(FrameworkElement.TagProperty, "Renamed"));
Defensive patterns

Strategy: validation

Validate before calling

if (property == FrameworkElement.NameProperty) throw new InvalidOperationException("Name cannot be set in a style");

Type guard

bool IsStyleable(DependencyProperty p) => p != null && !p.ReadOnly && p != FrameworkElement.NameProperty;

Try / catch

try { style.Setters.Add(new Setter(property, value)); } catch (InvalidOperationException) { /* property not allowed in style */ }

Prevention

When it happens

Trigger: new Setter(FrameworkElement.NameProperty, "foo") or Setter.Property = FrameworkElement.NameProperty in code; equivalent XAML would fail at parse time.

Common situations: Trying to rename elements conditionally via styles; generating styles programmatically over a property list that includes Name; template code attempting to change x:Name-like identity at runtime.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


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

Appendix: source

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

            // 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;

            if (dp == null)
            {
                throw new ArgumentException(SR.Format(SR.NullPropertyIllegal, "Setter.Property"));

View on GitHub (pinned to 81131a70a4)