Skip to content

Instantly share code, notes, and snippets.

@damirarh
Created July 14, 2013 09:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save damirarh/5993727 to your computer and use it in GitHub Desktop.
Save damirarh/5993727 to your computer and use it in GitHub Desktop.
public class EnumToImageConverter : IValueConverter
{
public string SubFolder { get; set; }
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is Enum && targetType == typeof(ImageSource))
{
var enumType = value.GetType();
var format = String.IsNullOrEmpty(SubFolder) ? "/Assets/{0}/{2}.png" : "/Assets/{0}/{1}/{2}.png";
var path = String.Format(format, enumType.Name, SubFolder, Enum.GetName(enumType, value));
var uri = new Uri(path, UriKind.Relative);
return new BitmapImage(uri);
}
return value;
}
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