Fix: colorpicker will crashed when SelectedBrush prop assigned to null

This commit is contained in:
chenjie02_cd committed 2021-07-04 14:34:20 +08:00
1 parent 6b3b58f47c
commit 8e912df45e
1 file changed
+11 -5
@@ -261,11 +261,17 @@ namespace HandyControl.Controls
}
public static readonly DependencyProperty SelectedBrushProperty = DependencyProperty.Register(
"SelectedBrush", typeof(SolidColorBrush), typeof(ColorPicker), new PropertyMetadata(Brushes.White,
(o, args) =>
"SelectedBrush", typeof(SolidColorBrush), typeof(ColorPicker), new PropertyMetadata(Brushes.White, OnSelectedBrushChanged, CoerceSelectedBrush));
private static object CoerceSelectedBrush(DependencyObject d, object basevalue)
{
var ctl = (ColorPicker) o;
var v = (SolidColorBrush) args.NewValue;
return basevalue is SolidColorBrush ? basevalue : Brushes.White;
}
private static void OnSelectedBrushChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var ctl = (ColorPicker) d;
var v = (SolidColorBrush) e.NewValue;
if (ctl.IsNeedUpdateInfo)
{
@@ -282,7 +288,7 @@ namespace HandyControl.Controls
{
Info = v.Color
});
}));
}
/// <summary>
/// 当前选中的颜色