PageRenderTime 34ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/CBR/CBR/Components/Converters/BoolToWindowStateConverter.cs

#
C# | 83 lines | 51 code | 6 blank | 26 comment | 11 complexity | 3cef3ac57948e78a2ac205e98b2a63cc MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows.Data;
  6. namespace CBR.Components.Converters
  7. {
  8. /// <summary>
  9. /// Converter : Boolean to System.Windows.Visibility and not revert
  10. /// </summary>
  11. [ValueConversion(typeof(bool), typeof(System.Windows.WindowState))]
  12. public class BoolToWindowStateConverter : IValueConverter
  13. {
  14. /// <summary>
  15. /// Singleton access
  16. /// </summary>
  17. public static readonly BoolToWindowStateConverter Instance = new BoolToWindowStateConverter();
  18. /// <summary>
  19. /// Private constructor for singleton pattern
  20. /// </summary>
  21. private BoolToWindowStateConverter()
  22. {
  23. }
  24. /// <summary>
  25. /// IValueConverter.Convert
  26. /// </summary>
  27. /// <param name="value"></param>
  28. /// <param name="targetType"></param>
  29. /// <param name="parameter"></param>
  30. /// <param name="culture"></param>
  31. /// <returns></returns>
  32. public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  33. {
  34. bool val = false;
  35. bool param = true;
  36. if (value != null)
  37. val = (bool)value;
  38. if (parameter != null)
  39. param = System.Convert.ToBoolean(parameter);
  40. if (val)
  41. {
  42. // allow to test on !value, that is not possible in the binding
  43. if( param == true )
  44. return System.Windows.WindowState.Maximized;
  45. else
  46. return System.Windows.WindowState.Normal;
  47. }
  48. else
  49. {
  50. if (param == true)
  51. return System.Windows.WindowState.Normal;
  52. else
  53. return System.Windows.WindowState.Maximized;
  54. }
  55. }
  56. /// <summary>
  57. /// IValueConverter.ConvertBack
  58. /// </summary>
  59. /// <param name="value"></param>
  60. /// <param name="targetType"></param>
  61. /// <param name="parameter"></param>
  62. /// <param name="culture"></param>
  63. /// <returns></returns>
  64. public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  65. {
  66. System.Windows.WindowState val = (System.Windows.WindowState)value;
  67. if (val == System.Windows.WindowState.Maximized)
  68. {
  69. return true;
  70. }
  71. else
  72. {
  73. return false;
  74. }
  75. }
  76. }
  77. }