dotnet/wpf · error · NotSupportedException
SR.Converter_ConvertToNotSupported
Error message
SR.Converter_ConvertToNotSupported
What it means
BrushConverter.ConvertTo throws NotSupportedException when the target type is string but the Brush instance reports CanSerializeToString() == false. Only certain brush types (e.g. SolidColorBrush with a simple color) can be expressed as a string; others cannot round-trip and the converter refuses rather than emit a lossy value.
Solutions
- Check brush.CanSerializeToString() before calling ConvertTo and fall back to full XAML/BAML serialization for complex brushes.
- Convert the brush to a resource reference or serialized form instead of a plain string.
- If you only need simple brushes, restrict inputs to SolidColorBrush instances.
- Handle NotSupportedException and use XamlWriter.Save as fallback.
Example fix
// before
string s = (string)brushConverter.ConvertTo(ctx, culture, gradientBrush, typeof(string)); // throws
// after
string s = gradientBrush.CanSerializeToString()
? (string)brushConverter.ConvertTo(ctx, culture, gradientBrush, typeof(string))
: XamlWriter.Save(gradientBrush); Defensive patterns
Strategy: validation
Validate before calling
if (brush is SolidColorBrush scb) { /* safe to convert */ } else { bool convertible = ((Brush)brush).CanSerializeToString(); } Type guard
bool canToString = brush is Brush b && b.CanSerializeToString();
Try / catch
try { s = (string)converter.ConvertTo(ctx, culture, brush, typeof(string)); } catch (NotSupportedException) { s = XamlWriter.Save(brush); } Prevention
- Check CanSerializeToString() before ConvertTo.
- Use XAML serialization for gradient/complex brushes.
- Avoid stringifying brushes for persistence; store resources instead.
- Restrict settings models to SolidColorBrush.
When it happens
Trigger: Calling ConvertTo(context, culture, value, typeof(string)) where value is a Brush whose CanSerializeToString() returns false (e.g. many gradient/complex brushes) while a context with a non-null Instance is supplied.
Common situations: Serializing a LinearGradientBrush/RadialGradientBrush to a string for settings storage or code generation; XAML designer/serialization engines attempting string conversion of complex brushes.
Understand the failure class
Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.
Related errors
- SR.Converter_ConvertToNotSupported
- Converter_ConvertToNotSupported
- SR.Converter_ConvertToNotSupported
- SR.Converter_ConvertToNotSupported
- SR.Converter_ConvertToNotSupported
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/37bcbd8f9b0f00c5.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Generated/BrushConverter.cs:137
/// </exception>
/// <param name="context"> The ITypeDescriptorContext for this call. </param>
/// <param name="culture"> The CultureInfo which is respected when converting. </param>
/// <param name="value"> The object to convert to an instance of "destinationType". </param>
/// <param name="destinationType"> The type to which this will convert the Brush instance. </param>
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if (destinationType != null && value is Brush)
{
Brush instance = (Brush)value;
if (destinationType == typeof(string))
{
// When invoked by the serialization engine we can convert to string only for some instances
if (context != null && context.Instance != null)
{
if (!instance.CanSerializeToString())
{
throw new NotSupportedException(SR.Converter_ConvertToNotSupported);
}
}
// Delegate to the formatting/culture-aware ConvertToString method.
return instance.ConvertToString(null, culture);
}
}
// Pass unhandled cases to base class (which will throw exceptions for null value or destinationType.)
return base.ConvertTo(context, culture, value, destinationType);
}
}
}
View on GitHub (pinned to 81131a70a4)