{"record":{"id":"f20aeda5333d4efa","repo":"dotnet/maui","slug":"cannot-convert-null-into-typeof-buttoncontentlayo","errorCode":null,"errorMessage":"Cannot convert null into {typeof(ButtonContentLayout)}","messagePattern":"Cannot convert null into (.+?)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Controls/src/Core/Button/Button.cs","lineNumber":585,"sourceCode":"\t\t}\n\n\t\t/// <summary>\n\t\t/// A converter to convert a string to a <see cref=\"ButtonContentLayout\"/> object.\n\t\t/// </summary>\n\t\tpublic sealed class ButtonContentTypeConverter : TypeConverter\n\t\t{\n\t\t\tpublic override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)\n\t\t\t\t=> sourceType == typeof(string);\n\n\t\t\tpublic override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)\n\t\t\t\t=> false;\n\n\t\t\tpublic override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)\n\t\t\t{\n\t\t\t\t// IMPORTANT! Update ButtonContentDesignTypeConverter.IsValid if making changes here\n\t\t\t\tvar strValue = value?.ToString();\n\t\t\t\tif (strValue == null)\n\t\t\t\t\tthrow new InvalidOperationException($\"Cannot convert null into {typeof(ButtonContentLayout)}\");\n\n\t\t\t\tstring[] parts = strValue.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);\n\n\t\t\t\tif (parts.Length != 1 && parts.Length != 2)\n\t\t\t\t\tthrow new InvalidOperationException($\"Cannot convert \\\"{strValue}\\\" into {typeof(ButtonContentLayout)}\");\n\n\t\t\t\tdouble spacing = DefaultSpacing;\n\t\t\t\tvar position = ButtonContentLayout.ImagePosition.Left;\n\n\t\t\t\tvar spacingFirst = char.IsDigit(parts[0][0]);\n\n\t\t\t\tint positionIndex = spacingFirst ? (parts.Length == 2 ? 1 : -1) : 0;\n\t\t\t\tint spacingIndex = spacingFirst ? 0 : (parts.Length == 2 ? 1 : -1);\n\n\t\t\t\tif (spacingIndex > -1)\n\t\t\t\t\tspacing = double.Parse(parts[spacingIndex], CultureInfo.InvariantCulture);\n\n\t\t\t\tif (positionIndex > -1)","sourceCodeStart":567,"sourceCodeEnd":603,"githubUrl":"https://github.com/dotnet/maui/blob/f377ff1c5ee04d334d8a925f50c83a6b7afddf03/src/Controls/src/Core/Button/Button.cs#L567-L603","documentation":"Thrown by ButtonContentLayoutTypeConverter.ConvertFrom when value?.ToString() returns null. The converter can only parse string representations of a ButtonContentLayout (e.g. the content layout XAML attribute); a null input has no string form and cannot be converted.","triggerScenarios":"Passing null to the converter. A XAML attribute resolving to null (binding evaluates to null, or a StaticResource missing). Programmatic TypeConverter.ConvertFrom(null) invocation.","commonSituations":"Binding the ContentLayout property to a source whose value becomes null. XAML with a missing/empty attribute routed through the converter. Serialization round-trips that lose the value.","solutions":["Provide a non-null string value in the format expected (e.g. \"Left,10\" or \"Top\").","Guard the source so it never evaluates to null before it reaches the converter.","Use a fallback/default ButtonContentLayout instance instead of null."],"exampleFix":"// before\nbutton.ContentLayout = (ButtonContentLayout)\n    new ButtonContentTypeConverter().ConvertFrom(null, culture, null);\n\n// after\nbutton.ContentLayout = new ButtonContentLayout(ButtonContentLayout.ImagePosition.Left, 10);","handlingStrategy":"validation","validationCode":"if (value is null) return new ButtonContentLayout(ButtonContentLayout.ImagePosition.Left, ButtonContentLayout.DefaultSpacing);\n// otherwise proceed with converter","typeGuard":"static bool CanConvertToButtonContentLayout(object v) =>\n    v is string s && !string.IsNullOrWhiteSpace(s);","tryCatchPattern":null,"preventionTips":["Never pass null to the ContentLayout converter.","Guard bindings so the source never evaluates to null.","Use a default ButtonContentLayout instead of null."],"tags":["maui","button","type-converter","xaml","null"],"backgroundTag":null,"analyzedSha":"f377ff1c5ee04d334d8a925f50c83a6b7afddf03","analyzedAt":"2026-08-13T14:26:18.069Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}