{"record":{"id":"35cd0a7f662492ad","repo":"dotnet/wpf","slug":"sr-format-sr-cannothavepropertyinstyle-frameworkelement","errorCode":null,"errorMessage":"SR.Format(SR.CannotHavePropertyInStyle, FrameworkElement.NameProperty.Name)","messagePattern":"SR\\.Format\\(SR\\.CannotHavePropertyInStyle, FrameworkElement\\.NameProperty\\.Name\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Setter.cs","lineNumber":72,"sourceCode":"            // No null check for target since null is a valid value.\n\n            _property = property;\n            _value = value;\n            _target = target;\n        }\n\n        private void CheckValidProperty( DependencyProperty property)\n        {\n            ArgumentNullException.ThrowIfNull(property);\n            if (property.ReadOnly)\n            {\n                // Read-only properties will not be consulting Style/Template/Trigger Setter for value.\n                //  Rather than silently do nothing, throw error.\n                throw new ArgumentException(SR.Format(SR.ReadOnlyPropertyNotAllowed, property.Name, GetType().Name));\n            }\n            if( property == FrameworkElement.NameProperty)\n            {\n                throw new InvalidOperationException(SR.Format(SR.CannotHavePropertyInStyle, FrameworkElement.NameProperty.Name));\n            }\n        }\n\n        /// <summary>\n        ///     Seals this setter\n        /// </summary>\n        internal override void Seal()\n        {\n\n            // Do the validation that can't be done until we know all of the property\n            // values.\n\n            DependencyProperty dp = Property;\n            object value = ValueInternal;\n\n            if (dp == null)\n            {\n                throw new ArgumentException(SR.Format(SR.NullPropertyIllegal, \"Setter.Property\"));","sourceCodeStart":54,"sourceCodeEnd":90,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Setter.cs#L54-L90","documentation":"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.","triggerScenarios":"new Setter(FrameworkElement.NameProperty, \"foo\") or Setter.Property = FrameworkElement.NameProperty in code; equivalent XAML would fail at parse time.","commonSituations":"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.","solutions":["Remove the Setter targeting NameProperty","Set the name at element construction/XAML declaration (x:Name) instead","If conditional naming is needed, create distinct elements or use Tag/attached properties for state"],"exampleFix":"// before\nstyle.Setters.Add(new Setter(FrameworkElement.NameProperty, \"Renamed\"));\n// after\nstyle.Setters.Add(new Setter(FrameworkElement.TagProperty, \"Renamed\"));","handlingStrategy":"validation","validationCode":"if (property == FrameworkElement.NameProperty) throw new InvalidOperationException(\"Name cannot be set in a style\");","typeGuard":"bool IsStyleable(DependencyProperty p) => p != null && !p.ReadOnly && p != FrameworkElement.NameProperty;","tryCatchPattern":"try { style.Setters.Add(new Setter(property, value)); } catch (InvalidOperationException) { /* property not allowed in style */ }","preventionTips":["Never include FrameworkElement.NameProperty in generated setter lists","Set names via x:Name at declaration time; use Tag or attached properties for runtime state"],"tags":["wpf","setter","style","namescope"],"backgroundTag":"unsupported-operation","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}