Skip to content

Commit

Permalink
(MahAppsGH-4019) Implement ColorToSolidColorBrushConverter
Browse files Browse the repository at this point in the history
This should resolve the binding issues.
  • Loading branch information
timunie authored and punker76 committed Feb 25, 2021
1 parent 711f987 commit da7f973
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 20 deletions.
56 changes: 56 additions & 0 deletions src/MahApps.Metro/Converters/ColorToSolidColorBrushConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
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;

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
{
static ColorToSolidColorBrushConverter _DefaultInstance;

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

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

protected override 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;
}
}

protected override object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is SolidColorBrush brush)
{
return brush.Color;
}
else
{
return null;
}
}
}
}
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 Mode=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

0 comments on commit da7f973

Please sign in to comment.