{"record":{"id":"0b89d1fe1b4a11bf","repo":"dotnet/wpf","slug":"sr-format-sr-cannotconverttype-typeof-propertypath","errorCode":null,"errorMessage":"SR.Format(SR.CannotConvertType, typeof(PropertyPath), destinationType.FullName)","messagePattern":"SR\\.Format\\(SR\\.CannotConvertType, typeof\\(PropertyPath\\), destinationType\\.FullName\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/PropertyPathConverter.cs","lineNumber":133,"sourceCode":"        /// An ArgumentException is thrown if the example object is not null and is not a Brush,\n        /// or if the destinationType isn't one of the valid destination types.\n        /// </exception>\n        /// <param name=\"typeDescriptorContext\"> The ITypeDescriptorContext for this call.\n        ///  If this is null, then no namespace prefixes will be included.</param>\n        /// <param name=\"cultureInfo\"> The CultureInfo which is respected when converting. </param>\n        /// <param name=\"value\"> The PropertyPath to convert. </param>\n        /// <param name=\"destinationType\">The type to which to convert the PropertyPath instance. </param>\n        public override object ConvertTo(ITypeDescriptorContext typeDescriptorContext,\n                                         CultureInfo cultureInfo,\n                                         object value,\n                                         Type destinationType)\n        {\n            ArgumentNullException.ThrowIfNull(value);\n            ArgumentNullException.ThrowIfNull(destinationType);\n\n            if (destinationType != typeof(String))\n            {\n                throw new ArgumentException(SR.Format(SR.CannotConvertType, typeof(PropertyPath), destinationType.FullName));\n            }\n\n            PropertyPath path = value as PropertyPath;\n            if (path == null)\n            {\n                throw new ArgumentException(SR.Format(SR.UnexpectedParameterType, value.GetType(), typeof(PropertyPath)), nameof(value));\n            }\n\n            if (path.PathParameters.Count == 0)\n            {\n                // if the path didn't use paramaters, just write it out as it is\n                return path.Path;\n            }\n            else\n            {\n                // if the path used parameters, convert them to (NamespacePrefix:OwnerType.DependencyPropertyName) syntax\n                string originalPath = path.Path;\n                Collection<object> parameters = path.PathParameters;","sourceCodeStart":115,"sourceCodeEnd":151,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/PropertyPathConverter.cs#L115-L151","documentation":"PropertyPathConverter.ConvertTo throws this ArgumentException when asked to convert a PropertyPath to a destination type other than String. The converter only supports converting to string (the path's Path property); value must also be a PropertyPath, otherwise a separate UnexpectedParameterType error is thrown.","triggerScenarios":"Calling ConvertTo(value, typeof(int)) or any non-string destinationType; serialization frameworks requesting a different representation of a PropertyPath; XAML writers requesting unsupported output types in custom scenarios.","commonSituations":"Custom serializers or designers converting PropertyPath to other types; copying conversion code that assumed general TypeConverter support; automation that converts every property value to its native type rather than string.","solutions":["Convert to string: (string)converter.ConvertTo(null, culture, path, typeof(string)), or use path.Path directly","If a different type is needed, convert to string first and then parse/format that string to the target type","Fix the caller so destinationType is typeof(string) when handling PropertyPath values","Guard the call site: only invoke ConvertTo when destinationType == typeof(string)"],"exampleFix":"// before\nvar s = (int)converter.ConvertTo(null, null, path, typeof(int)); // throws\n// after\nvar s = (string)converter.ConvertTo(null, null, path, typeof(string));\n// or use the path string directly\nvar s = path.Path;","handlingStrategy":"type-guard","validationCode":"bool safe = destinationType == typeof(string) && value is PropertyPath;\nif (!safe) { /* use path.Path or a different conversion strategy */ }","typeGuard":"bool CanConvertToString(object v, Type dest) => dest == typeof(string) && v is PropertyPath;","tryCatchPattern":"try { var s = converter.ConvertTo(null, culture, path, destinationType); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"Cannot convert\")) { s = ((PropertyPath)path).Path; }","preventionTips":["Only request typeof(string) as the destination for PropertyPath conversion","Use path.Path directly instead of the converter when you control the code path","Check destinationType before invoking ConvertTo in generic serialization loops"],"tags":["wpf","typeconverter","property-path","unsupported-conversion"],"backgroundTag":"unsupported-operation","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}