-
Notifications
You must be signed in to change notification settings - Fork 807
Expand file tree
/
Copy pathEnumToIntConverter.cs
More file actions
61 lines (56 loc) · 3 KB
/
EnumToIntConverter.cs
File metadata and controls
61 lines (56 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
using System;
using System.Globalization;
using System.Windows.Data;
namespace NETworkManager.Converters;
/// <summary>
/// A value converter that facilitates the conversion between enumeration values and their corresponding integer indices or string names.
/// </summary>
/// <remarks>
/// This converter is designed to either:
/// - Convert an enumeration value to its integer index within the enumeration.
/// - Convert an enumeration value to its string representation.
/// When converting back, the same logic is applied to handle appropriate conversion.
/// </remarks>
public class EnumToIntConverter : IValueConverter
{
/// <summary>
/// Converts an Enum value to its corresponding integer index or string representation
/// based on the target type. If the target type is an Enum, the method attempts
/// to return the name of the enum value. If the provided value is an Enum, it
/// returns the integer index of the value within the Enum's definition.
/// </summary>
/// <param name="value">The value to convert. This can be an Enum instance or null.</param>
/// <param name="targetType">The target type for the conversion. Typically either Enum or int.</param>
/// <param name="parameter">An optional parameter for additional input, not used in this method.</param>
/// <param name="culture">The culture information for the conversion process.</param>
/// <returns>
/// If the targetType is an Enum, a string representation of the Enum name is returned.
/// If the value is an Enum, the integer index of the Enum value is returned.
/// If neither condition is met, null is returned.
/// </returns>
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (targetType.IsEnum)
{
return value is null ? string.Empty :
Enum.GetName(targetType, value);
}
if (value is Enum enumVal)
{
return Array.IndexOf(Enum.GetValues(enumVal.GetType()), enumVal);
}
return null;
}
/// Converts a value back into its corresponding enumeration value or integer representation.
/// This method is typically used in two-way data bindings where an integer or string needs
/// to be converted back to the corresponding enum value.
/// <param name="value">The value to be converted back. This can be an integer or a string representation of an enumeration value.</param>
/// <param name="targetType">The type of the target object, typically the type of the enumeration.</param>
/// <param name="parameter">An optional parameter for the conversion. Not used in this implementation.</param>
/// <param name="culture">The culture information to use during the conversion process.</param>
/// <return>The enumeration value corresponding to the input value.</return>
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return Convert(value, targetType, parameter, culture);
}
}