Fix: colorpicker will crashed when SelectedBrush prop assigned to null
This commit is contained in:
1 parent
6b3b58f47c
commit
8e912df45e
1 file changed
+27
-21
@@ -261,28 +261,34 @@ 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) =>
|
|
||||||
{
|
|
||||||
var ctl = (ColorPicker) o;
|
|
||||||
var v = (SolidColorBrush) args.NewValue;
|
|
||||||
|
|
||||||
if (ctl.IsNeedUpdateInfo)
|
private static object CoerceSelectedBrush(DependencyObject d, object basevalue)
|
||||||
{
|
{
|
||||||
ctl.IsNeedUpdateInfo = false;
|
return basevalue is SolidColorBrush ? basevalue : Brushes.White;
|
||||||
ctl.ChannelR = v.Color.R;
|
}
|
||||||
ctl.ChannelG = v.Color.G;
|
|
||||||
ctl.ChannelB = v.Color.B;
|
private static void OnSelectedBrushChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
||||||
ctl.ChannelA = v.Color.A;
|
{
|
||||||
ctl.IsNeedUpdateInfo = true;
|
var ctl = (ColorPicker) d;
|
||||||
}
|
var v = (SolidColorBrush) e.NewValue;
|
||||||
ctl.UpdateStatus(v.Color);
|
|
||||||
ctl.SelectedBrushWithoutOpacity = new SolidColorBrush(Color.FromRgb(v.Color.R, v.Color.G, v.Color.B));
|
if (ctl.IsNeedUpdateInfo)
|
||||||
ctl.RaiseEvent(new FunctionEventArgs<Color>(SelectedColorChangedEvent, ctl)
|
{
|
||||||
{
|
ctl.IsNeedUpdateInfo = false;
|
||||||
Info = v.Color
|
ctl.ChannelR = v.Color.R;
|
||||||
});
|
ctl.ChannelG = v.Color.G;
|
||||||
}));
|
ctl.ChannelB = v.Color.B;
|
||||||
|
ctl.ChannelA = v.Color.A;
|
||||||
|
ctl.IsNeedUpdateInfo = true;
|
||||||
|
}
|
||||||
|
ctl.UpdateStatus(v.Color);
|
||||||
|
ctl.SelectedBrushWithoutOpacity = new SolidColorBrush(Color.FromRgb(v.Color.R, v.Color.G, v.Color.B));
|
||||||
|
ctl.RaiseEvent(new FunctionEventArgs<Color>(SelectedColorChangedEvent, ctl)
|
||||||
|
{
|
||||||
|
Info = v.Color
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 当前选中的颜色
|
/// 当前选中的颜色
|
||||||
|
|||||||
Reference in new issue
Block a user