{"record":{"id":"62d5bf9c7ae85444","repo":"dotnet/wpf","slug":"sr-settervaluecannotbeunset","errorCode":null,"errorMessage":"SR.SetterValueCannotBeUnset","messagePattern":"SR\\.SetterValueCannotBeUnset","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Setter.cs","lineNumber":49,"sourceCode":"            Initialize( property, value, null );\n        }\n\n        /// <summary>\n        ///     Property Setter construction - given property, value, and string identifier for child node.\n        /// </summary>\n        public Setter( DependencyProperty property, object value, string targetName )\n        {\n            Initialize( property, value, targetName );\n        }\n\n        /// <summary>\n        ///     Method that does all the initialization work for the constructors.\n        /// </summary>\n        private void Initialize( DependencyProperty property, object value, string target )\n        {\n            if( value == DependencyProperty.UnsetValue )\n            {\n                throw new ArgumentException(SR.SetterValueCannotBeUnset);\n            }\n\n            CheckValidProperty(property);\n\n            // 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.","sourceCodeStart":31,"sourceCodeEnd":67,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Setter.cs#L31-L67","documentation":"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.","triggerScenarios":"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.","commonSituations":"Programmatically building styles from property values read via GetValue (which can return UnsetValue); refactoring code that used UnsetValue as a 'clear' sentinel.","solutions":["Pass a real value (or use a different mechanism such as removing the Setter)","Skip creating the Setter when the source value is DependencyProperty.UnsetValue","Guard constructor inputs before building the style"],"exampleFix":"// before\nobject v = target.GetValue(prop);\nstyle.Setters.Add(new Setter(prop, v)); // v may be UnsetValue\n// after\nobject v = target.GetValue(prop);\nif (v != DependencyProperty.UnsetValue)\n    style.Setters.Add(new Setter(prop, v));","handlingStrategy":"validation","validationCode":"if (value == DependencyProperty.UnsetValue) return; // skip instead of constructing Setter","typeGuard":"bool IsValidSetterValue(object v) => v != null && v != DependencyProperty.UnsetValue;","tryCatchPattern":"try { var s = new Setter(prop, value); } catch (ArgumentException) { /* value was UnsetValue */ }","preventionTips":["Check GetValue results for UnsetValue before feeding them into Setters","Never use UnsetValue as a 'clear' sentinel for Setter"],"tags":["wpf","setter","style","unset-value"],"backgroundTag":"invalid-argument-value","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}