EnumToBooleanConverter.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. namespace GDNXFD.Alert.Config.Converters
  2. {
  3. using System;
  4. using System.Windows;
  5. using System.Windows.Data;
  6. /// <summary>
  7. /// enum to bool converter
  8. /// </summary>
  9. public class EnumToBooleanConverter : IValueConverter
  10. {
  11. /// <summary>
  12. /// Convert a travel type to bool.
  13. /// </summary>
  14. /// <param name="value"></param>
  15. /// <param name="targetType"></param>
  16. /// <param name="parameter"></param>
  17. /// <param name="culture"></param>
  18. /// <returns></returns>
  19. public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  20. {
  21. if (value != null)
  22. {
  23. string parameterString = parameter as string;
  24. if (parameterString == null)
  25. return DependencyProperty.UnsetValue;
  26. if (Enum.IsDefined(value.GetType(), value) == false)
  27. return DependencyProperty.UnsetValue;
  28. object parameterValue = Enum.Parse(value.GetType(), parameterString);
  29. return parameterValue.Equals(value);
  30. }
  31. return null;
  32. }
  33. /// <summary>
  34. /// Convert back
  35. /// </summary>
  36. /// <param name="value"></param>
  37. /// <param name="targetType"></param>
  38. /// <param name="parameter"></param>
  39. /// <param name="culture"></param>
  40. /// <returns></returns>
  41. public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  42. {
  43. if (value != null && (bool)value)
  44. {
  45. string parameterString = parameter as string;
  46. if (!string.IsNullOrEmpty(parameterString))
  47. return Enum.Parse(targetType, parameterString);
  48. }
  49. return DependencyProperty.UnsetValue;
  50. }
  51. }
  52. }