Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions src/MahApps.Metro/Converters/ColorToSolidColorBrushConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System;
using System.Globalization;
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 : IValueConverter
{
private static ColorToSolidColorBrushConverter defaultInstance;

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

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

/// <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;
}

return this.FallbackBrush;
}

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

<ContentControl x:Name="PART_NoColorPreview"
Expand Down
6 changes: 1 addition & 5 deletions src/MahApps.Metro/Themes/ColorPicker/ColorPalette.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,7 @@
<Binding Path="ColorNamesDictionary" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=mah:ColorPalette}" />
</MultiBinding>
</Border.ToolTip>
<Grid>
<Grid.Background>
<SolidColorBrush Color="{Binding}" />
</Grid.Background>
</Grid>
<Grid Background="{Binding Converter={x:Static converters:ColorToSolidColorBrushConverter.DefaultInstance}}" />
</Border>
</DataTemplate>

Expand Down
12 changes: 2 additions & 10 deletions src/MahApps.Metro/Themes/ColorPicker/ColorPicker.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,7 @@
Background="{DynamicResource MahApps.Brushes.Tile.Small}"
BorderBrush="{DynamicResource MahApps.Brushes.Control.Border}"
BorderThickness="1">
<Rectangle>
<Rectangle.Fill>
<SolidColorBrush Color="{Binding TargetNullValue=Transparent}" />
</Rectangle.Fill>
</Rectangle>
<Rectangle Fill="{Binding Converter={x:Static converters:ColorToSolidColorBrushConverter.DefaultInstance}}" />
</Border>

<TextBlock Grid.Column="1"
Expand All @@ -49,11 +45,7 @@
Background="{DynamicResource MahApps.Brushes.Tile.Small}"
BorderBrush="{DynamicResource MahApps.Brushes.Control.Border}"
BorderThickness="1">
<Rectangle>
<Rectangle.Fill>
<SolidColorBrush Color="{Binding}" />
</Rectangle.Fill>
</Rectangle>
<Rectangle Fill="{Binding Converter={x:Static converters:ColorToSolidColorBrushConverter.DefaultInstance}}" />
</Border>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding}" Value="{x:Null}">
Expand Down