public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            string description = value as string;

            if (description == null)
            {
                return(value);
            }

            if ((targetType == null) || !targetType.IsEnum)
            {
                return(value);
            }

            Tuple <object[], string[]> tuple = EnumValueDescriptionCache.GetValues(targetType);

            for (int i = 0; i < tuple.Item2.Length; i++)
            {
                if (description == tuple.Item2[i])
                {
                    return(tuple.Item1[i]);
                }
            }

            return(value);
        }
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null)
            {
                return(null);
            }

            Tuple <object[], string[]> tuple = EnumValueDescriptionCache.GetValues(value.GetType());

            for (int n = 0; n < tuple.Item1.Length; n++)
            {
                if (Equals(tuple.Item1[n], value))
                {
                    if (targetType == typeof(object))
                    {
                        return(tuple.Item2[n]); // property binding
                    }
                    if (targetType == typeof(IEnumerable))
                    {
                        return(tuple.Item2); // ItemsSource binding
                    }

                    Debug.Assert(false, "Oops, better check your binding");
                }
            }

            return(value);
        }