stride3d/stride · error · ArgumentException

The given value must implement IEnumerable

Error message

The given value must implement IEnumerable

What it means

CountEnumerable.Convert converts a binding value to the number of items it contains. If the value is neither null nor an IEnumerable, the converter cannot count it and throws ArgumentException. This guards against binding to non-collection values (e.g. a scalar or string-bound property of incompatible type).

Solutions

  1. Make the bound property implement IEnumerable (e.g. expose an ObservableCollection<T> or List<T>).
  2. If a null/empty value is expected, return null instead of a non-enumerable so the converter returns 0.
  3. Wrap the binding in a fallback or use a different converter if counting non-collections makes no sense.
  4. Catch ArgumentException in code that calls the converter directly with unknown values.

Example fix

// before
_myViewModel.Count = 5; // bound with CountEnumerable converter
// after
_myViewModel.Items = new ObservableCollection<int>(Enumerable.Range(0, 5));
Defensive patterns

Strategy: validation

Validate before calling

bool isCountable(object? v) => v is null || v is System.Collections.IEnumerable;

Type guard

static bool IsEnumerable(object? v, out System.Collections.IEnumerable? e) { e = v as System.Collections.IEnumerable; return e != null || v == null; }

Try / catch

try { count = converter.Convert(value, typeof(int), null, CultureInfo.InvariantCulture); } catch (ArgumentException ex) { /* log; fall back to 0 */ }

Prevention

When it happens

Trigger: Calling Convert(value, ...) with a non-null value that does not implement IEnumerable, e.g. Convert(42, typeof(int), null, CultureInfo.InvariantCulture) or binding a scalar property to a control using this converter.

Common situations: XAML data-binding mistakes where a scalar property is bound to ItemsControl-like consumers expecting a collection; refactors that changed a property from List<T> to a plain value type; unit tests passing arbitrary objects directly to the converter.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of stride3d/stride@96fad776d2 (2026-09-14). Data as JSON: /api/errors/40fb11cbd25e1529. Report an issue: GitHub.

Appendix: source

Thrown at sources/presentation/Stride.Core.Presentation.Wpf/ValueConverters/CountEnumerable.cs:25

using Stride.Core.Annotations;

namespace Stride.Core.Presentation.ValueConverters
{
    /// <summary>
    /// This converter will take an enumerable as input and return the number of items it contains.
    /// </summary>
    public class CountEnumerable : OneWayValueConverter<CountEnumerable>
    {
        /// <inheritdoc/>
        [NotNull]
        public override object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null)
                return 0;

            var enumerable = value as IEnumerable;
            if (enumerable == null)
                throw new ArgumentException(@"The given value must implement IEnumerable", nameof(value));

            return (value as ICollection)?.Count ?? enumerable.Cast<object>().Count();
        }
    }
}

View on GitHub (pinned to 96fad776d2)