Multiprocotol Terminalprogram (BAT)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
MultiTerm/MultiTerm.Wpf/ValueConverters/EnumDescriptionConverter.cs

60 lines
1.9 KiB

using System;
using System.ComponentModel;
using System.Globalization;
using System.Reflection;
using System.Windows.Data;
namespace MultiTerm.Wpf.ValueConverters;
public class EnumDescriptionConverter : IValueConverter
{
private string GetEnumDescription(Enum enumObject)
{
FieldInfo fieldInfo = enumObject.GetType().GetField(enumObject.ToString());
object[] attributeArray = fieldInfo.GetCustomAttributes(false);
if(attributeArray.Length == 0)
{
return enumObject.ToString();
}
else
{
DescriptionAttribute attribute = attributeArray[0] as DescriptionAttribute;
return attribute.Description;
}
}
/// <summary>
/// Convert from Data Source to Dependency Object type.
/// Here => Enum type to object.
/// </summary>
/// <param name="value"></param>
/// <param name="targetType"></param>
/// <param name="parameter"></param>
/// <param name="culture"></param>
/// <returns></returns>
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
Enum myEnum = (Enum)value;
string description = this.GetEnumDescription(myEnum);
return description;
}
/// <summary>
/// Convert from Dependency Object type to Data Source type.
/// Here => object to Enum type.
/// </summary>
/// <param name="value"></param>
/// <param name="targetType"></param>
/// <param name="parameter"></param>
/// <param name="culture"></param>
/// <returns></returns>
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
int returnValue = 0;
if (parameter is Type)
{
returnValue = (int)Enum.Parse((Type)parameter, value.ToString());
}
return returnValue;
}
}