initial commit
This commit is contained in:
32
Converters/HexColorToBrushConverter.cs
Normal file
32
Converters/HexColorToBrushConverter.cs
Normal file
@@ -0,0 +1,32 @@
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using System.Windows.Data;
|
||||
using System.Windows.Media;
|
||||
|
||||
namespace HC_APTBS.Converters
|
||||
{
|
||||
/// <summary>
|
||||
/// Converts a CSS-style hex colour string (e.g. "#26C200") to a
|
||||
/// <see cref="SolidColorBrush"/> for use in WPF bindings.
|
||||
/// Returns <see cref="Brushes.Transparent"/> for null or invalid input.
|
||||
/// </summary>
|
||||
[ValueConversion(typeof(string), typeof(SolidColorBrush))]
|
||||
public sealed class HexColorToBrushConverter : IValueConverter
|
||||
{
|
||||
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
|
||||
{
|
||||
if (value is string hex && !string.IsNullOrWhiteSpace(hex))
|
||||
{
|
||||
try
|
||||
{
|
||||
return new SolidColorBrush((Color)ColorConverter.ConvertFromString(hex));
|
||||
}
|
||||
catch { /* fall through */ }
|
||||
}
|
||||
return Brushes.Transparent;
|
||||
}
|
||||
|
||||
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
|
||||
=> throw new NotSupportedException();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user