{"record":{"id":"3762b763fbd4c38e","repo":"MahApps/MahApps.Metro","slug":"cannot-convert-the-given-input-to-a-valid-color","errorCode":null,"errorMessage":"Cannot convert the given input to a valid color","messagePattern":"Cannot convert the given input to a valid color","errorType":"exception","errorClass":"InvalidCastException","httpStatus":null,"severity":"error","filePath":"src/MahApps.Metro/Controls/ColorPicker/ColorPickerBase.cs","lineNumber":139,"sourceCode":"                    {\n                        colorPicker.SetCurrentValue(SelectedColorProperty, color);\n                    }\n                    else // if the color stayed the same we still have to update the displayed name\n                    {\n                        colorPicker.ColorIsUpdating = true;\n                        try\n                        {\n                            colorPicker.SetCurrentValue(ColorNameProperty, (colorPicker.ColorHelper ?? ColorHelper.DefaultInstance).GetColorName(color, colorPicker.ColorNamesDictionary, colorPicker.IsAlphaChannelVisible));\n                        }\n                        finally\n                        {\n                            colorPicker.ColorIsUpdating = false;\n                        }\n                    }\n                }\n                else\n                {\n                    throw new InvalidCastException(\"Cannot convert the given input to a valid color\");\n                }\n            }\n        }\n\n        /// <summary>\n        /// Gets or sets the name of the <see cref=\"SelectedColor\"/>.\n        /// </summary>\n        public string? ColorName\n        {\n            get => (string?)this.GetValue(ColorNameProperty);\n            set => this.SetValue(ColorNameProperty, value);\n        }\n\n        /// <summary>Identifies the <see cref=\"ColorNamesDictionary\"/> dependency property.</summary>\n        public static readonly DependencyProperty ColorNamesDictionaryProperty\n            = DependencyProperty.Register(nameof(ColorNamesDictionary),\n                                          typeof(Dictionary<Color, string>),\n                                          typeof(ColorPickerBase),","sourceCodeStart":121,"sourceCodeEnd":157,"githubUrl":"https://github.com/MahApps/MahApps.Metro/blob/72099e310bac2d12ac98fd7560b69679252519f5/src/MahApps.Metro/Controls/ColorPicker/ColorPickerBase.cs#L121-L157","documentation":"ColorPickerBase.OnColorNamePropertyChanged fires when the bound ColorName string changes. It asks the configured ColorHelper to parse the string; when parsing yields no color (the helper returns null), the callback throws InvalidCastException. This rejects any string the helper cannot map to a known color name or hex value.","triggerScenarios":"ColorName is set (via binding or directly) to a value that ColorHelper.ColorFromString cannot parse — neither a recognized name nor a valid hex string — so the helper returns null and the callback throws InvalidCastException during the dependency-property change notification.","commonSituations":"User types an unrecognized color name in a bound TextBox. Binding feeds a localized name not present in ColorNamesDictionary. A hex string with the wrong format/length (#RGB vs #ARGB) for the configured IsAlphaChannelVisible. A custom ColorHelper that returns null for valid inputs.","solutions":["Validate the input string before assigning to ColorName — ensure it parses to a Color via the same ColorHelper/ColorNamesDictionary first.","Provide a ColorNamesDictionary that includes the localized/custom names you accept, or restrict the input to known names.","Set IsAlphaChannelVisible to match the hex format you feed in (e.g. #AARRGGBB when alpha is visible).","Supply a custom ColorHelper that can parse your color name format instead of returning null."],"exampleFix":"// before: unknown name throws inside the DP callback\nColorNameTextBox.TextChanged += (s,e) => vm.ColorName = ColorNameTextBox.Text;\n// after: validate first\nvar helper = ColorHelper.DefaultInstance;\nvar parsed = helper.ColorFromString(input, vm.ColorNamesDictionary);\nif (parsed is not null) vm.ColorName = input;\nelse vm.ColorError = $\"'{input}' is not a valid color\";","handlingStrategy":"validation","validationCode":"// Validate the string can be parsed by the same helper before assigning ColorName\nvar helper = colorPicker.ColorHelper ?? ColorHelper.DefaultInstance;\nvar color = helper.ColorFromString(input, colorPicker.ColorNamesDictionary);\nif (color is null) {\n    // show user-facing error, do NOT assign to ColorName\n    return;\n}\ncolorPicker.ColorName = input;","typeGuard":null,"tryCatchPattern":"try {\n    colorPicker.ColorName = input;\n} catch (InvalidCastException) {\n    // input not parseable; keep previous color, surface a user message\n}","preventionTips":["Pre-validate color strings with the same ColorHelper + ColorNamesDictionary before binding.","Constrain user input to a known list of color names or a hex pattern matching IsAlphaChannelVisible.","Provide a ColorNamesDictionary that includes localized names you accept.","Keep hex format consistent with IsAlphaChannelVisible (#RRGGBB vs #AARRGGBB)."],"tags":["wpf","colorpicker","binding","validation","xaml","runtime"],"backgroundTag":null,"analyzedSha":"72099e310bac2d12ac98fd7560b69679252519f5","analyzedAt":"2026-08-13T20:19:34.419Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}