@ -1,10 +1,8 @@
using System ;
using System ;
using System.ComponentModel ;
using System.Globalization ;
using System.Globalization ;
using System.Reflection ;
using System.Windows.Data ;
using System.Windows.Data ;
using System.Collections.Generic ;
using System.Windows.Controls ;
using System.Windows.Controls ;
using Common.Helpers ;
namespace MultiTerm.Wpf.ValueConverters ;
namespace MultiTerm.Wpf.ValueConverters ;
@ -16,65 +14,6 @@ namespace MultiTerm.Wpf.ValueConverters;
[ValueConversion(typeof(Enum), typeof(MenuItem))]
[ValueConversion(typeof(Enum), typeof(MenuItem))]
public class EnumDescriptionToMenuItemConverter : IValueConverter
public class EnumDescriptionToMenuItemConverter : IValueConverter
{
{
/// <summary>
/// Gets the description of an Enum value.
/// If there is no Description set, the Enum Value will be converted to string.
/// </summary>
/// <param name="enumObject">Enum Object to get Description of.</param>
/// <returns>String with Content of DescriptionAttribute of Enum object.</returns>
private static string GetEnumDescription ( Enum enumObject )
{
// guard argument null
if ( enumObject = = null ) { throw new ArgumentNullException ( nameof ( enumObject ) ) ; }
// get field info from enum type
FieldInfo ? fieldInfo = enumObject . GetType ( ) . GetField ( enumObject . ToString ( ) ) ;
// return string of enum value if there is no field info
if ( fieldInfo = = null )
{
return enumObject . ToString ( ) ;
}
// get description attribute and return if it is present
DescriptionAttribute ? descAttrib = ( DescriptionAttribute ? ) fieldInfo . GetCustomAttribute ( typeof ( DescriptionAttribute ) , true ) ;
if ( descAttrib ! = null )
{
return descAttrib . Description ;
}
// if no description attribute was found => return string of enum value
return enumObject . ToString ( ) ;
}
/// <summary>
/// Gets all Description Attributes of a Enum type.
/// Descriptions must be unique!
/// </summary>
/// <param name="enumObject">An object of the Enum. Will be searched for other Descriptions</param>
/// <returns>Key Value Pair of Description and respective Enum Value</returns>
private static Dictionary < string , Enum > GetAllEnumDescriptions ( Enum enumObject )
{
// guard argument null
if ( enumObject = = null ) { throw new ArgumentNullException ( nameof ( enumObject ) ) ; }
Dictionary < string , Enum > descriptionsToEnumValues = new ( ) ;
// get members of enum type
var members = enumObject . GetType ( ) . GetMembers ( ) ;
foreach ( var member in members )
{
// get description attributes of all members
DescriptionAttribute ? descAttrib = ( DescriptionAttribute ? ) member . GetCustomAttribute ( typeof ( DescriptionAttribute ) , true ) ;
if ( descAttrib ! = null )
{
// if a description exists, add the description and the enum value to the dictionary
descriptionsToEnumValues . Add ( descAttrib . Description , ( Enum ) Enum . Parse ( enumObject . GetType ( ) , member . Name ) ) ;
}
}
return descriptionsToEnumValues ;
}
/// <summary>
/// <summary>
/// Convert from Data Source to Dependency Object type.
/// Convert from Data Source to Dependency Object type.
/// Here: Enum type to MenuItem.
/// Here: Enum type to MenuItem.
@ -87,7 +26,7 @@ public class EnumDescriptionToMenuItemConverter : IValueConverter
public object Convert ( object value , Type targetType , object parameter , CultureInfo culture )
public object Convert ( object value , Type targetType , object parameter , CultureInfo culture )
{
{
Enum enumValue = ( Enum ) value ;
Enum enumValue = ( Enum ) value ;
string description = GetEnumDescription ( enumValue ) ;
string description = EnumHelpers . GetEnumDescription ( enumValue ) ;
MenuItem newMenuItem = new ( )
MenuItem newMenuItem = new ( )
{
{
Header = description
Header = description
@ -106,8 +45,6 @@ public class EnumDescriptionToMenuItemConverter : IValueConverter
/// <returns>Enum value of the MenuItem that was converted</returns>
/// <returns>Enum value of the MenuItem that was converted</returns>
public object ConvertBack ( object value , Type targetType , object parameter , CultureInfo culture )
public object ConvertBack ( object value , Type targetType , object parameter , CultureInfo culture )
{
{
Enum ? enumObj ;
// guard argument null
// guard argument null
if ( targetType = = null ) { throw new ArgumentNullException ( nameof ( targetType ) ) ; }
if ( targetType = = null ) { throw new ArgumentNullException ( nameof ( targetType ) ) ; }
if ( value = = null ) { throw new ArgumentNullException ( nameof ( value ) ) ; }
if ( value = = null ) { throw new ArgumentNullException ( nameof ( value ) ) ; }
@ -118,15 +55,9 @@ public class EnumDescriptionToMenuItemConverter : IValueConverter
throw new Exception ( $"Cannot convert value that is not of type {nameof(MenuItem)} with {nameof(EnumDescriptionToMenuItemConverter)}" ) ;
throw new Exception ( $"Cannot convert value that is not of type {nameof(MenuItem)} with {nameof(EnumDescriptionToMenuItemConverter)}" ) ;
}
}
// generate instance of enum using target type
enumObj = Activator . CreateInstance ( targetType ) as Enum ;
if ( enumObj = = null )
{
throw new Exception ( $"Could not instanciate Enum of targetType {targetType}." ) ;
}
// get all enum descriptions and iterate
// get all enum descriptions and iterate
var descriptionToEnumValue = GetAllEnumDescriptions ( enumObj ) ;
var enumObj = EnumHelpers . CreateInstanceOfEnumType ( targetType ) ;
var descriptionToEnumValue = EnumHelpers . GetAllEnumDescriptions ( enumObj ) ;
foreach ( var kvp in descriptionToEnumValue )
foreach ( var kvp in descriptionToEnumValue )
{
{
// compare key (enum description) to menu header
// compare key (enum description) to menu header