lucasg/Dependencies · error · ArgumentException

Can only convert to string.

Error message

Can only convert to string.

What it means

Thrown by EnumToStringUsingDescription.ConvertTo (DependenciesGui/DependencyWindow.xaml.cs:183) when the WPF binding infrastructure requests a conversion to any type other than System.String. This TypeConverter exists solely to render enums as their [Description] text in the UI, so non-string targets are rejected with ArgumentException(parameterName="destinationType").

Source

Thrown at DependenciesGui/DependencyWindow.xaml.cs:183

        {
            return (sourceType.Equals(typeof(Enum)));
        }

        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
        {
            return (destinationType.Equals(typeof(String)));
        }

        public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
        {
            return base.ConvertFrom(context, culture, value);
        }

        public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
        {
            if (!destinationType.Equals(typeof(String)))
            {
                throw new ArgumentException("Can only convert to string.", "destinationType");
            }

            if (!value.GetType().BaseType.Equals(typeof(Enum)))
            {
                throw new ArgumentException("Can only convert an instance of enum.", "value");
            }

            string name = value.ToString();
            object[] attrs =
                value.GetType().GetField(name).GetCustomAttributes(typeof(DescriptionAttribute), false);
            return (attrs.Length > 0) ? ((DescriptionAttribute)attrs[0]).Description : name;
        }
    }

    /// <summary>
    /// User context of every dependency tree node.
    /// </summary>
    public struct DependencyNodeContext

View on GitHub (pinned to 1997a40000)

Solutions

  1. Check the binding/template that targets the enum and ensure it only needs a string representation (the converter's CanConvertTo already reports only String).
  2. If a non-string conversion is genuinely needed, use a different converter rather than this description-rendering one.
  3. Catch ArgumentException at the call site if the converter is invoked reflectively and degrade to value.ToString().

Example fix

// before: invoking the converter for an arbitrary type
var text = conv.ConvertTo(null, culture, enumValue, typeof(int)); // throws
// after: guard with CanConvertTo
var targetType = typeof(int);
var text = conv.CanConvertTo(null, targetType)
    ? conv.ConvertTo(null, culture, enumValue, targetType)
    : enumValue.ToString();
Defensive patterns

Strategy: type-guard

Validate before calling

// Consult CanConvertTo before ConvertTo; it already reports only String.
if (!conv.CanConvertTo(null, destinationType))
{
    return value.ToString(); // graceful fallback instead of ArgumentException
}
return conv.ConvertTo(null, culture, value, destinationType);

Type guard

bool CanConvertToString(Type t) => t == typeof(string);

Try / catch

try { return conv.ConvertTo(context, culture, value, destinationType); }
catch (ArgumentException) when (!destinationType.Equals(typeof(string)))
{
    return value?.ToString();
}

Prevention

When it happens

Trigger: A XAML binding or PropertyGrid asks the converter to produce a non-string representation (e.g. via a custom control requesting the enum's underlying type, or an invalid Binding converter parameter).

Common situations: Custom XAML templates that re-template an enum-bound control and request an exotic target type; programmatic misuse of the converter against a TypeDescriptor chain.

Related errors


AI-assisted analysis of lucasg/Dependencies@1997a40000 (2026-08-13). Data as JSON: /api/errors/732064005104e370. Report an issue: GitHub.