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( public static readonly DependencyProperty SelectedBrushProperty = DependencyProperty.Register(
"SelectedBrush", typeof(SolidColorBrush), typeof(ColorPicker), new PropertyMetadata(Brushes.White, "SelectedBrush", typeof(SolidColorBrush), typeof(ColorPicker), new PropertyMetadata(Brushes.White, OnSelectedBrushChanged, CoerceSelectedBrush));
(o, args) =>
private static object CoerceSelectedBrush(DependencyObject d, object basevalue)
{ {
var ctl = (ColorPicker) o; return basevalue is SolidColorBrush ? basevalue : Brushes.White;
var v = (SolidColorBrush) args.NewValue; }
private static void OnSelectedBrushChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var ctl = (ColorPicker) d;
var v = (SolidColorBrush) e.NewValue;
if (ctl.IsNeedUpdateInfo) if (ctl.IsNeedUpdateInfo)
{ {
@@ -282,7 +288,7 @@ namespace HandyControl.Controls
{ {
Info = v.Color Info = v.Color
}); });
})); }
/// <summary> /// <summary>
/// 当前选中的颜色 /// 当前选中的颜色