Skip to content

Commit

Permalink
(MahAppsGH-4019) Implement IValueConverter directly
Browse files Browse the repository at this point in the history
  • Loading branch information
punker76 authored and batzen committed Mar 3, 2021
1 parent 96c5e33 commit 6de0f4c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 26 deletions.
45 changes: 20 additions & 25 deletions src/MahApps.Metro/Converters/ColorToSolidColorBrushConverter.cs
Original file line number Diff line number Diff line change
@@ -1,56 +1,51 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;
using System.Windows.Media;
using JetBrains.Annotations;

namespace MahApps.Metro.Converters
{
/// <summary>
/// Converts a given <see cref="Color"/> into a <see cref="SolidColorBrush"/>.
/// </summary>
[ValueConversion (typeof(Color), typeof(SolidColorBrush)) ]
public class ColorToSolidColorBrushConverter : MarkupConverter
[ValueConversion(typeof(Color), typeof(SolidColorBrush))]
public class ColorToSolidColorBrushConverter : IValueConverter
{
static ColorToSolidColorBrushConverter _DefaultInstance;
private static ColorToSolidColorBrushConverter defaultInstance;

/// <summary>
/// returns a static instance if needed.
/// Gets a static instance of the converter if needed.
/// </summary>
public static ColorToSolidColorBrushConverter DefaultInstance => _DefaultInstance ??= new ColorToSolidColorBrushConverter();
public static ColorToSolidColorBrushConverter DefaultInstance => defaultInstance ??= new ColorToSolidColorBrushConverter();

/// <summary>
/// Gets or Sets the FallbackBrush which should be used if the conversion fails.
/// Gets or Sets the brush which will be used if the conversion fails.
/// </summary>
[CanBeNull]
public SolidColorBrush FallbackBrush { get; set; }

protected override object Convert(object value, Type targetType, object parameter, CultureInfo culture)
/// <summary>
/// Gets or Sets the color which will be used if the conversion fails.
/// </summary>
[CanBeNull]
public Color? FallbackColor { get; set; }

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is Color color)
{
var brush = new SolidColorBrush(color);
brush.Freeze();
return brush;
}
else
{
return FallbackBrush;
}

return this.FallbackBrush;
}

protected override object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is SolidColorBrush brush)
{
return brush.Color;
}
else
{
return null;
}
return value is SolidColorBrush brush ? brush.Color : this.FallbackColor;
}
}
}
}
2 changes: 1 addition & 1 deletion src/MahApps.Metro/Themes/ColorPicker/ColorCanvas.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@
Background="{DynamicResource MahApps.Brushes.Tile}"
BorderBrush="{DynamicResource MahApps.Brushes.ThemeForeground}"
BorderThickness="1">
<Rectangle Fill="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=SelectedColor, Converter={x:Static converters:ColorToSolidColorBrushConverter.DefaultInstance}}" />
<Rectangle Fill="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=SelectedColor, Converter={x:Static converters:ColorToSolidColorBrushConverter.DefaultInstance}}" />
</Border>

<ContentControl x:Name="PART_NoColorPreview"
Expand Down

0 comments on commit 6de0f4c

Please sign in to comment.