-
-
Save gimmebytes/0d217c2261ce7ea60f33a2ae2db93958 to your computer and use it in GitHub Desktop.
MahApps icon to Image converter
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
using System; | |
using System.Globalization; | |
using System.Windows.Data; | |
using System.Windows.Media; | |
using MahApps.Metro.IconPacks; | |
namespace SomeNamespace | |
{ | |
public class IconConverter : IValueConverter | |
{ | |
private const PackIconFontAwesomeKind OkState = PackIconFontAwesomeKind.QuestionCircle; | |
private const PackIconFontAwesomeKind MissingState = PackIconFontAwesomeKind.CircleOutline; | |
private static readonly Brush OkStateBrush = Brushes.DarkGreen; | |
private static readonly Brush MissingStateBrush = Brushes.DarkRed; | |
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) | |
{ | |
var state = value as bool? ?? false; | |
return GetIconForState(state); | |
} | |
private ImageSource GetIconForState(bool state) | |
{ | |
var packIcon = new PackIconFontAwesome() | |
{ | |
Kind = state ? OkState : MissingState | |
}; | |
var brush = state ? OkStateBrush : MissingStateBrush; | |
var geometryDrawing = new GeometryDrawing | |
{ | |
Geometry = Geometry.Parse(packIcon.Data), | |
Brush = brush, | |
Pen = new Pen(brush, 0.25), | |
}; | |
var drawingGroup = new DrawingGroup | |
{ | |
Children = {geometryDrawing}, | |
Transform = new ScaleTransform(1, -1) | |
}; | |
return new DrawingImage { Drawing = drawingGroup }; | |
} | |
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) | |
{ | |
throw new NotImplementedException(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment