System.Windows.Controls.EnumToBooleanConverter.ConvertBack C# (CSharp) Method

ConvertBack() public method

Converts a boolean value back to an enum value.
public ConvertBack ( object value, Type targetType, object parameter, CultureInfo culture ) : object
value object The value that is to be converted.
targetType Type The type to which the value is to be converted. In this case it is an enum type.
parameter object A parameter whose enum value is returned.
culture System.Globalization.CultureInfo The culture information of the current culture, so that parsing can be adjusted to cultural conventions.
return object
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            // Determines whether the target type is nullable
            bool isNullable = targetType.Name == typeof(Nullable<>).Name;
            Type enumerationType = isNullable ? targetType.GenericTypeArguments.FirstOrDefault() : targetType;

            // Gets the parameter and check if it is null, if so then the default value for the target type is returned
            string enumValueName = parameter as string;
            if (enumValueName == null)
                throw new ArgumentException("The parameter was not set.");

            // Converts the parameter to its target value
            if ((bool)value)
                return Enum.Parse(enumerationType, enumValueName);
            else
                return DependencyProperty.UnsetValue;
        }
EnumToBooleanConverter