forked from MahApps/MahApps.Metro
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(MahAppsGH-4019) Implement ColorToSolidColorBrushConverter
This should resolve the binding issues.
- Loading branch information
Showing
4 changed files
with
60 additions
and
20 deletions.
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
src/MahApps.Metro/Converters/ColorToSolidColorBrushConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters