Skip to content

Instantly share code, notes, and snippets.

@gimmebytes
Last active October 24, 2017 15:44
Show Gist options
  • Save gimmebytes/0d217c2261ce7ea60f33a2ae2db93958 to your computer and use it in GitHub Desktop.
Save gimmebytes/0d217c2261ce7ea60f33a2ae2db93958 to your computer and use it in GitHub Desktop.
MahApps icon to Image converter
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