{"record":{"id":"b8e6f9e5d7214de1","repo":"dotnet/wpf","slug":"sr-mustbeoftype-0-must-be-of-type-1-args-value","errorCode":null,"errorMessage":"SR.MustBeOfType: '{0}' must be of type '{1}'. (args: value, TemplateBindingExpression)","messagePattern":"SR\\.MustBeOfType: '(.+?)' must be of type '(.+?)'\\. \\(args: value, TemplateBindingExpression\\)","errorType":"exception","errorClass":"System.ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/TemplateBindingExpressionConverter.cs","lineNumber":45,"sourceCode":"        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)\n        {\n            if (destinationType == typeof(MarkupExtension))\n            {\n                return true;\n            }\n            return base.CanConvertTo(context, destinationType);\n        }\n\n        /// <summary>\n        /// Converts to a MarkupExtension\n        /// </summary>\n        public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)\n        {\n            if (destinationType == typeof(MarkupExtension))\n            {\n                TemplateBindingExpression templateBindingExpression = value as TemplateBindingExpression;\n                if (templateBindingExpression == null)\n                    throw new ArgumentException(SR.Format(SR.MustBeOfType, \"value\", \"TemplateBindingExpression\"));\n                return templateBindingExpression.TemplateBindingExtension;\n            }\n            return base.ConvertTo(context, culture, value, destinationType);\n        }\n    }\n\n\n}\n\n\n","sourceCodeStart":27,"sourceCodeEnd":56,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/TemplateBindingExpressionConverter.cs#L27-L56","documentation":"TemplateBindingExpressionConverter.ConvertTo converts a TemplateBindingExpression to a MarkupExtension, and throws ArgumentException (SR.MustBeOfType) when the supplied value is not actually a TemplateBindingExpression. This is a defensive type check on the converter's input.","triggerScenarios":"Calling ConvertTo (directly or via TypeDescriptor/serialization) with destinationType == typeof(MarkupExtension) and a value that is not a TemplateBindingExpression — e.g. null or another expression type.","commonSituations":"XAML serialization/designer tooling or custom code invoking the converter with a wrongly-typed value; passing a BindingExpression where a TemplateBindingExpression is expected.","solutions":["Pass a TemplateBindingExpression instance (e.g. obtained from reading a template's markup) to ConvertTo.","Null-check / type-check the value before calling the converter.","If converting other expression types, use their own dedicated converters (e.g. BindingExpression converters)."],"exampleFix":"// before\nconverter.ConvertTo(context, culture, someOtherExpression, typeof(MarkupExtension));\n// after\nif (someOtherExpression is TemplateBindingExpression tbe)\n    converter.ConvertTo(context, culture, tbe, typeof(MarkupExtension));","handlingStrategy":"type-guard","validationCode":"if (value is not TemplateBindingExpression) throw new InvalidOperationException(\"converter requires TemplateBindingExpression\");","typeGuard":"static bool IsTemplateBindingExpression(object v) => v is TemplateBindingExpression;","tryCatchPattern":"try { result = converter.ConvertTo(ctx, culture, value, typeof(MarkupExtension)); }\ncatch (ArgumentException ex) { log(ex.Message); result = null; }","preventionTips":["Type-check values before routing them to a specific TypeConverter","Match expression types to their dedicated converters","Never pass null to ConvertTo"],"tags":["wpf","type-converter","argument","type-mismatch"],"backgroundTag":"type-mismatch","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}