28 lines
798 B
C#
28 lines
798 B
C#
using System;
|
|
using System.Windows.Data;
|
|
|
|
namespace JD.WPF.Converters
|
|
{
|
|
public class EnumToIntConverter : IValueConverter
|
|
{
|
|
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
|
|
{
|
|
return (int)value;
|
|
}
|
|
|
|
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
|
|
{
|
|
if (!targetType.IsEnum)
|
|
return Binding.DoNothing;
|
|
|
|
int intValue = (int)value;
|
|
foreach (var enumValue in targetType.GetEnumValues())
|
|
{
|
|
if ((int)enumValue == intValue)
|
|
return enumValue;
|
|
}
|
|
|
|
return Binding.DoNothing;
|
|
}
|
|
}
|
|
}
|