dotnet/maui · error · InvalidOperationException

Cannot convert "{0}" into {1}

Error message

Cannot convert "{0}" into {1}

What it means

Thrown by ColumnDefinitionCollectionTypeConverter.ConvertFrom when the incoming value is null or cannot be coerced to a string. The converter expects a comma-separated list of GridLength values (e.g. "*,Auto,100"); a null input has no string representation.

Source

Thrown at src/Controls/src/Core/ColumnDefinitionCollectionTypeConverter.cs:24

using System.Text;
using Microsoft.Maui.Controls.Xaml;

namespace Microsoft.Maui.Controls
{
	/// <summary>Converts a comma-separated string of grid lengths to a <see cref="ColumnDefinitionCollection"/>.</summary>
	[ProvideCompiled("Microsoft.Maui.Controls.XamlC.ColumnDefinitionCollectionTypeConverter")]
	public class ColumnDefinitionCollectionTypeConverter : TypeConverter
	{
		public override bool CanConvertFrom(ITypeDescriptorContext? context, Type sourceType)
			=> sourceType == typeof(string);

		public override bool CanConvertTo(ITypeDescriptorContext? context, Type? destinationType)
			=> destinationType == typeof(string);

		public override object? ConvertFrom(ITypeDescriptorContext? context, CultureInfo? culture, object value)
		{
			var strValue = value as string ?? value?.ToString()
				?? throw new InvalidOperationException(string.Format("Cannot convert \"{0}\" into {1}", value, typeof(ColumnDefinitionCollection)));

			// fast path for no value or empty string
			if (strValue.Length == 0)
				return new ColumnDefinitionCollection();

#if NET6_0_OR_GREATER
			var unsplit = (ReadOnlySpan<char>)strValue;
			var count = unsplit.Count(',') + 1;
			var definitions = new List<ColumnDefinition>(count);
			foreach (var range in unsplit.Split(','))
			{
				var length = Converters.GridLengthTypeConverter.ParseStringToGridLength(unsplit[range]);
				definitions.Add(new ColumnDefinition(length));
			}
#else
			var lengths = strValue.Split(',');
			var count = lengths.Length;
			var definitions = new List<ColumnDefinition>(count);

View on GitHub (pinned to f377ff1c5e)

Solutions

  1. Provide a valid comma-separated GridLength string (e.g. "*,Auto").
  2. Guard the source so it never yields null.
  3. Use a default ColumnDefinitionCollection when the input is unavailable.

Example fix

// before
var cols = (ColumnDefinitionCollection)
    new ColumnDefinitionCollectionTypeConverter().ConvertFrom(null);

// after
grid.ColumnDefinitions = new ColumnDefinitionCollection
{
    new ColumnDefinition(GridLength.Star),
    new ColumnDefinition(GridLength.Auto)
};
Defensive patterns

Strategy: validation

Validate before calling

if (value is null || value.ToString() is null)
    return new ColumnDefinitionCollection(); // or throw with context
// proceed with converter

Type guard

static bool IsValidColumnDefinitions(object v) =>
    v is string s && s.Length > 0;

Prevention

When it happens

Trigger: Passing null or a non-string object whose ToString() is null to the converter. A XAML ColumnDefinitions attribute bound to a source that evaluates to null. Programmatic ConvertFrom(null) calls.

Common situations: Binding ColumnDefinitions to a nullable property that becomes null. Missing XAML attribute resolved through the converter. Deserialization dropping the value.

Related errors


AI-assisted analysis of dotnet/maui@f377ff1c5e (2026-08-13). Data as JSON: /api/errors/77f98ef78e85125a. Report an issue: GitHub.