{"record":{"id":"bec772e473780fdc","repo":"dotnet/wpf","slug":"sr-parser-empty","errorCode":null,"errorMessage":"SR.Parser_Empty","messagePattern":"SR\\.Parser_Empty","errorType":"exception","errorClass":"FormatException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Parsers.cs","lineNumber":249,"sourceCode":"        }\n\n        /// <summary>\n        /// ParseBrush\n        /// <param name=\"brush\"> string with brush description </param>\n        /// <param name=\"formatProvider\">IFormatProvider for processing string</param>\n        /// <param name=\"context\">ITypeDescriptorContext</param>\n        /// </summary>\n        internal static Brush ParseBrush(string brush, IFormatProvider formatProvider, ITypeDescriptorContext context)\n        {\n            bool isPossibleKnownColor;\n            bool isNumericColor;\n            bool isScRgbColor;\n            bool isContextColor;\n            string trimmedColor = KnownColors.MatchColor(brush, out isPossibleKnownColor, out isNumericColor, out isContextColor, out isScRgbColor);\n\n            if (trimmedColor.Length == 0)\n            {\n                throw new FormatException(SR.Parser_Empty);\n            }\n\n            // Note that because trimmedColor is exactly brush.Trim() we don't have to worry about\n            // extra tokens as we do with TokenizerHelper.  If we return one of the solid color\n            // brushes then the ParseColor routine (or ColorStringToKnownColor) matched the entire\n            // input.\n            if (isNumericColor)\n            {\n                return (new SolidColorBrush(ParseHexColor(trimmedColor)));\n            }\n\n            if (isContextColor)\n            {\n                return (new SolidColorBrush(ParseContextColor(trimmedColor, formatProvider, context)));\n            }\n\n            if (isScRgbColor)\n            {","sourceCodeStart":231,"sourceCodeEnd":267,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Parsers.cs#L231-L267","documentation":"ParseBrush treats its input as a possible color first: it calls KnownColors.MatchColor to trim and classify the string. If the trimmed string is empty (nothing to parse), it throws FormatException(SR.Parser_Empty) before any token matching. An empty string can never denote a Brush.","triggerScenarios":"BrushConverter.ConvertFrom / ConvertFromString called with \"\" or a whitespace-only string; XAML attributes like Background=\"\"; databound or config-supplied brush strings that are empty.","commonSituations":"Missing configuration values defaulted to empty strings; XAML designers emitting empty attributes; user-supplied settings saved as blank; string interpolation producing \"\" when a variable is null/empty.","solutions":["Check for null/whitespace before converting and skip setting the brush when empty.","In XAML, omit the attribute entirely instead of writing Background=\"\"; or use {x:Null} to intentionally clear a brush.","Provide a default brush (e.g. Brushes.Transparent) when the input is empty.","Sanitize configuration/loading code so empty values become null rather than \"\"."],"exampleFix":"// before\nvar brush = (Brush)new BrushConverter().ConvertFromString(settings.Color); // settings.Color == \"\" -> throws\n\n// after\nvar brush = string.IsNullOrWhiteSpace(settings.Color)\n    ? Brushes.Transparent\n    : (Brush)new BrushConverter().ConvertFromString(settings.Color);","handlingStrategy":"validation","validationCode":"Brush ParseBrushSafe(string s) =>\n    string.IsNullOrWhiteSpace(s) ? null : (Brush)new BrushConverter().ConvertFromString(s.Trim());","typeGuard":null,"tryCatchPattern":"if (string.IsNullOrWhiteSpace(s)) { return Brushes.Transparent; }\ntry { return (Brush)new BrushConverter().ConvertFromString(s); }\ncatch (FormatException) { return Brushes.Transparent; }","preventionTips":["Treat empty brush strings as 'unset' and skip conversion.","In XAML, omit the attribute or use {x:Null} instead of an empty string.","Coerce empty config values to null at load time."],"tags":["wpf","brush-parsing","empty-input","format-exception"],"backgroundTag":"empty-required-field","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"}