From ae0d726d9ea8160581a13c46078d222f3ff16dab Mon Sep 17 00:00:00 2001 From: karlmoriaty <3070687583@qq.com> Date: Thu, 12 Nov 2026 22:43:52 +0800 Subject: [PATCH] Guard BrushToHexConverter against non-brush input The converter casts its input straight to SolidColorBrush. At design time the DataContext of the hex labels can fall back to the mock view model the XAML designer builds for d:DesignInstance, so that cast throws and the designer reports the error described in #1411. Return null for anything that is not a SolidColorBrush; the runtime path always passes a brush, so its behaviour is unchanged. Assisted-by: DeepSeek:deepseek-v4.1-flash:WorkBuddy Signed-off-by: karlmoriaty <3070687583@qq.com> --- .../Converters/BrushToHexConverter.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/MaterialDesignDemo.Shared/Converters/BrushToHexConverter.cs b/src/MaterialDesignDemo.Shared/Converters/BrushToHexConverter.cs index 5e196432fe..16b77a589a 100644 --- a/src/MaterialDesignDemo.Shared/Converters/BrushToHexConverter.cs +++ b/src/MaterialDesignDemo.Shared/Converters/BrushToHexConverter.cs @@ -8,10 +8,9 @@ public class BrushToHexConverter : IValueConverter { public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture) { - if (value is null) return null; + if (value is not SolidColorBrush brush) return null; static string lowerHexString(int i) => i.ToString("X2").ToLower(); - var brush = (SolidColorBrush)value; string hex = lowerHexString(brush.Color.R) + lowerHexString(brush.Color.G) + lowerHexString(brush.Color.B);