dotnet/wpf · error · System.ArgumentException

SR.MustBeOfType: ' ' must be of type ' '. (args: value…

Error message

SR.MustBeOfType: '{0}' must be of type '{1}'. (args: value, TemplateBindingExtension)

What it means

TemplateBindingExtensionConverter.ConvertTo produces an InstanceDescriptor for serialization and requires the value to be a TemplateBindingExtension. Any other value (including null) triggers ArgumentNullException or ArgumentException (SR.MustBeOfType) naming the 'value' parameter.

Solutions

  1. Pass a TemplateBindingExtension instance with a valid Property to ConvertTo.
  2. Type-check (is TemplateBindingExtension) before invoking the converter.
  3. Use the correct converter for other markup extension types rather than routing everything through this one.

Example fix

// before
converter.ConvertTo(context, culture, expression, typeof(InstanceDescriptor));
// after
if (expression is TemplateBindingExtension ext)
    converter.ConvertTo(context, culture, ext, typeof(InstanceDescriptor));
Defensive patterns

Strategy: type-guard

Validate before calling

if (value is not TemplateBindingExtension) throw new ArgumentException("value must be TemplateBindingExtension", nameof(value));

Type guard

static bool IsTemplateBindingExtension(object v) => v is TemplateBindingExtension;

Try / catch

try { result = converter.ConvertTo(ctx, culture, value, typeof(InstanceDescriptor)); }
catch (ArgumentException ex) { log(ex.ParamName); result = null; }

Prevention

When it happens

Trigger: Calling ConvertTo with a destinationType of InstanceDescriptor but a value that is not a TemplateBindingExtension; also ConvertTo(null, ...) which trips ArgumentNullException.ThrowIfNull(value).

Common situations: Serialization/designer tooling invoking the converter with wrong-typed input; code mistaking TemplateBindingExpression for TemplateBindingExtension.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/b57f827c0ae87b7e. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/TemplateBindingExtensionConverter.cs:51

            {
                return true;
            }
            return base.CanConvertTo(context, destinationType);
        }

        /// <summary>
        /// Converts to an InstanceDescriptor
        /// </summary>
        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
            if (destinationType == typeof(InstanceDescriptor))
            {
                ArgumentNullException.ThrowIfNull(value);

                TemplateBindingExtension templateBinding = value as TemplateBindingExtension;

                if(templateBinding == null)
                    throw new ArgumentException(SR.Format(SR.MustBeOfType, "value", "TemplateBindingExtension"), nameof(value));

                return new InstanceDescriptor(typeof(TemplateBindingExtension).GetConstructor(new Type[] { typeof(DependencyProperty) }),
                    new object[] { templateBinding.Property });
            }
            return base.ConvertTo(context, culture, value, destinationType);
        }

    }
}

View on GitHub (pinned to 81131a70a4)