System.Windows.Controls.EnumToVisibilityConverter.Convert C# (CSharp) Method

Convert() public method

Converts the value parameter to a visibility value.
public Convert ( object value, Type targetType, object parameter, CultureInfo culture ) : object
value object The value of an enum that is to be converted.
targetType Type The type to which the value is to be converted. In this case it is always visibility.
parameter object A parameter that determines the value to which the value has to be equal to return visible.
culture System.Globalization.CultureInfo The culture information of the current culture, so that parsing can be adjusted to cultural conventions.
return object
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            // Checks if the value provided is null, if so then tell the converter that the value is unset
            if (value == null || !(value is Enum))
                return DependencyProperty.UnsetValue;

            // Gets the parameter and check if it is null, if so then tells the converter, that the value is unset
            string enumValueName = parameter as string;
            if (enumValueName == null)
                return DependencyProperty.UnsetValue;

            // Checks if the enum type, from which is to be converted defines the value that is to be converted to visibility, if not, then tells the converter, that the value is unset
            if (!Enum.IsDefined(value.GetType(), value))
                return DependencyProperty.UnsetValue;

            // Converts the parameter value to the enum type
            object parameterEnumValue = Enum.Parse(value.GetType(), enumValueName);

            // If the parameter equals the value that is to be converted, then returns visible, otherwise hidden is returned
            return parameterEnumValue.Equals(value) ? Visibility.Visible : Visibility.Collapsed;
        }
EnumToVisibilityConverter