Skip to content

Commit

Permalink
Merge pull request #3988 from MahApps/fix/3986-ColorPicker-InvalidCas…
Browse files Browse the repository at this point in the history
…tException-DefaultColor

Fix InvalidCastException at ColorPicker when a DefaultColor is set
  • Loading branch information
punker76 authored Dec 1, 2020
2 parents f0ba628 + ea03383 commit 821d15d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
</GroupBox>

<GroupBox Margin="5 5 5 0" Header="Color Canvas">
<mah:ColorCanvas x:Name="ColorCanvasExample" SelectedColor="Blue" />
<mah:ColorCanvas x:Name="ColorCanvasExample" DefaultColor="{DynamicResource MahApps.Colors.AccentBase}" />
</GroupBox>

<GroupBox Margin="5" Header="Color Picker">
Expand Down
8 changes: 0 additions & 8 deletions src/MahApps.Metro/Controls/ColorPicker/ColorPicker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -758,9 +758,6 @@ internal override void OnSelectedColorChanged(Color? oldValue, Color? newValue)
{
base.OnSelectedColorChanged(newValue, oldValue);

// Set color is Updating again
this.ColorIsUpdating = true;

this.PART_ColorPaletteAvailable?.SetCurrentValue(Selector.SelectedValueProperty, newValue);
this.PART_ColorPaletteStandard?.SetCurrentValue(Selector.SelectedValueProperty, newValue);
this.PART_ColorPaletteCustom01?.SetCurrentValue(Selector.SelectedValueProperty, newValue);
Expand All @@ -771,8 +768,6 @@ internal override void OnSelectedColorChanged(Color? oldValue, Color? newValue)
{
BuildInColorPalettes.AddColorToRecentColors(newValue, this.RecentColorPaletteItemsSource, BuildInColorPalettes.GetMaximumRecentColorsCount(this));
}

this.ColorIsUpdating = false;
}

private void ColorPalette_SelectionChanged(object sender, SelectionChangedEventArgs e)
Expand Down Expand Up @@ -841,10 +836,7 @@ private static void OnIsDropDownOpenChanged(DependencyObject d, DependencyProper

if (colorPicker.AddToRecentColorsTrigger == AddToRecentColorsTrigger.ColorPickerClosed && colorPicker.SelectedColor.HasValue)
{
// We Update something so we need to flag this
colorPicker.ColorIsUpdating = true;
BuildInColorPalettes.AddColorToRecentColors(colorPicker.SelectedColor, colorPicker.RecentColorPaletteItemsSource, BuildInColorPalettes.GetMaximumRecentColorsCount(colorPicker));
colorPicker.ColorIsUpdating = false;
}
}
}
Expand Down
47 changes: 26 additions & 21 deletions src/MahApps.Metro/Controls/ColorPicker/ColorPickerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,26 @@ public static readonly DependencyProperty SelectedColorProperty
= DependencyProperty.Register(nameof(SelectedColor),
typeof(Color?),
typeof(ColorPickerBase),
new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnSelectedColorPropertyChanged));
new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnSelectedColorPropertyChanged, CoerceSelectedColorProperty));

private static void OnSelectedColorPropertyChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
{
if (dependencyObject is ColorPickerBase colorPicker && e.OldValue != e.NewValue && !colorPicker.ColorIsUpdating)
{
colorPicker.ColorIsUpdating = true;
colorPicker.OnSelectedColorChanged(e.OldValue as Color?, e.NewValue as Color? ?? colorPicker.DefaultColor);
colorPicker.ColorIsUpdating = false;
}
}

private static object CoerceSelectedColorProperty(DependencyObject dependencyObject, object basevalue)
{
if (dependencyObject is ColorPickerBase colorPicker)
{
colorPicker.OnSelectedColorChanged(e.OldValue as Color?, e.NewValue as Color?);
basevalue ??= colorPicker.DefaultColor;
}

return basevalue;
}

/// <summary>
Expand All @@ -46,7 +58,15 @@ public static readonly DependencyProperty DefaultColorProperty
= DependencyProperty.Register(nameof(DefaultColor),
typeof(Color?),
typeof(ColorPickerBase),
new FrameworkPropertyMetadata(null, OnSelectedColorPropertyChanged));
new FrameworkPropertyMetadata(null, OnDefaultColorPropertyChanged));

private static void OnDefaultColorPropertyChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
{
if (dependencyObject is ColorPickerBase colorPicker && e.OldValue != e.NewValue)
{
colorPicker.SetCurrentValue(SelectedColorProperty, e.NewValue ?? colorPicker.SelectedColor);
}
}

/// <summary>
/// Gets or sets a default selected <see cref="Color"/>
Expand Down Expand Up @@ -411,25 +431,12 @@ public event RoutedPropertyChangedEventHandler<Color?> SelectedColorChanged

internal virtual void OnSelectedColorChanged(Color? oldValue, Color? newValue)
{
// don't do a second update
if (this.ColorIsUpdating)
{
return;
}

this.ColorIsUpdating = true;

if (this.SelectedColor == null && this.DefaultColor != null)
{
this.SetCurrentValue(SelectedColorProperty, this.DefaultColor);
}

this.SetCurrentValue(ColorNameProperty, ColorHelper.GetColorName(this.SelectedColor, this.ColorNamesDictionary));
this.SetCurrentValue(ColorNameProperty, ColorHelper.GetColorName(newValue, this.ColorNamesDictionary));

// We just update the following lines if we have a Color.
if (this.SelectedColor != null)
if (newValue != null)
{
var color = (Color)this.SelectedColor;
var color = (Color)newValue;

if (this.UpdateHsvValues)
{
Expand All @@ -447,8 +454,6 @@ internal virtual void OnSelectedColorChanged(Color? oldValue, Color? newValue)
this.SetCurrentValue(BProperty, color.B);
}

this.ColorIsUpdating = false;

this.RaiseEvent(new RoutedPropertyChangedEventArgs<Color?>(oldValue, newValue, SelectedColorChangedEvent));
}

Expand Down

0 comments on commit 821d15d

Please sign in to comment.