Skip to content

Instantly share code, notes, and snippets.

Created May 20, 2017 09:48
Show Gist options
  • Save anonymous/05563e32ba52eec834d7225c01cce8ff to your computer and use it in GitHub Desktop.
Save anonymous/05563e32ba52eec834d7225c01cce8ff to your computer and use it in GitHub Desktop.
public enum GestioneProprietaStraus
{
[Description("Applica")]
Applica,
[Description("Calcola Cerniere Plastiche")]
//[EnumDisplayName("Calcola Cerniere Plastiche")]
Calcola_Cerniere_Plastiche
}
public class GeneralSettings :INotifyPropertyChanged
{
//here some properties...
[PropertyOrder(9)]
[DisplayName("Gestione proprietà su Straus7 in")]
[System.ComponentModel.TypeConverter(typeof(EnumDescConverter))]
public GestioneProprietaStraus ManageStrausProperties
{
get { return _manageStrausProperties; }
set { _manageStrausProperties = value; NotifyPropertyChanged("ManageStrausProperties"); }
}
//here some other properties...
}
public class EnumDescConverter : EnumConverter
{
private Type _enumType;
/// Initializing instance
/// type Enum
///this is only one function, that you must
///to change. All another functions for enums
///you can use by Ctrl+C/Ctrl+V
public EnumDescConverter(Type type)
: base(type)
{
_enumType = type;
}
public override bool CanConvertTo(ITypeDescriptorContext context, Type destType)
{
return destType == typeof(string);
}
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destType)
{
FieldInfo fi = _enumType.GetField(Enum.GetName(_enumType, value));
DescriptionAttribute dna = (DescriptionAttribute)Attribute.GetCustomAttribute(fi, typeof(DescriptionAttribute));
if (dna != null)
return dna.Description;
else
return value.ToString();
}
public override bool CanConvertFrom(ITypeDescriptorContext context, Type srcType)
{
return srcType == typeof(string);
}
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
foreach (FieldInfo fi in _enumType.GetFields())
{
DescriptionAttribute dna =
(DescriptionAttribute)Attribute.GetCustomAttribute(fi, typeof(DescriptionAttribute));
if ((dna != null) && ((string)value == dna.Description))
return Enum.Parse(_enumType, fi.Name);
}
return Enum.Parse(_enumType, (string)value);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment