BoolToVisibilityConverterExtension.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Text;
  5. using System.Windows;
  6. using System.Windows.Data;
  7. using System.Windows.Markup;
  8. namespace Flyinsono.Client.Test.Extensions
  9. {
  10. [Flags]
  11. public enum BoolToVisibilityEnums
  12. {
  13. /// <summary>
  14. /// True=Visible,False=Collapsed
  15. /// </summary>
  16. None = 0x0,
  17. /// <summary>
  18. /// True=invisible,False=visible
  19. /// </summary>
  20. Revert = 0x2,
  21. /// <summary>
  22. /// invisible=Visibility.Hidden
  23. /// </summary>
  24. InvisibleHidden = 0x4,
  25. RevertHidden = Revert | InvisibleHidden
  26. }
  27. [MarkupExtensionReturnType(typeof(IValueConverter))]
  28. public class BoolToVisibilityConverterExtension : MarkupExtension, IValueConverter, IMultiValueConverter
  29. {
  30. #region Properties 
  31. [ThreadStatic]
  32. private static BoolToVisibilityConverterExtension _converter;
  33. public static BoolToVisibilityConverterExtension Converter
  34. {
  35. get { return _converter ?? (_converter = new BoolToVisibilityConverterExtension()); }
  36. }
  37. #endregion Properties 
  38. #region Methods 
  39. // Constructors 
  40. public BoolToVisibilityConverterExtension()
  41. {
  42. }
  43. // Methods 
  44. public override object ProvideValue(IServiceProvider serviceProvider)
  45. {
  46. return Converter;
  47. }
  48. public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  49. {
  50. var boolValue = ToBool(value);
  51. bool isRevert;
  52. Visibility invisible;
  53. TryParseParameter(parameter, out invisible, out isRevert);
  54. if (isRevert)
  55. {
  56. boolValue = !boolValue;
  57. }
  58. return boolValue ? Visibility.Visible : invisible;
  59. }
  60. public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  61. {
  62. var visibility = (Visibility)value;
  63. return visibility == Visibility.Visible;
  64. }
  65. public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
  66. {
  67. bool boolValue = false;
  68. if (values != null && values.Length > 0)
  69. {
  70. bool hasInvisible = false;
  71. foreach (var value in values)
  72. {
  73. if (!ToBool(value))
  74. {
  75. hasInvisible = true;
  76. break;
  77. }
  78. }
  79. boolValue = !hasInvisible;
  80. }
  81. bool isRevert;
  82. Visibility invisible;
  83. TryParseParameter(parameter, out invisible, out isRevert);
  84. if (isRevert)
  85. {
  86. boolValue = !boolValue;
  87. }
  88. return boolValue ? Visibility.Visible : invisible;
  89. }
  90. public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
  91. {
  92. throw new NotImplementedException();
  93. }
  94. private bool ToBool(object value)
  95. {
  96. var boolValue = false;
  97. if (value != null)
  98. {
  99. if (value is Visibility)
  100. {
  101. boolValue = (Visibility)value == Visibility.Visible;
  102. }
  103. else
  104. {
  105. string valueInString = value.ToString();
  106. if (string.IsNullOrEmpty(valueInString) || !bool.TryParse(valueInString, out boolValue))
  107. {
  108. boolValue = false;
  109. }
  110. }
  111. }
  112. return boolValue;
  113. }
  114. private void TryParseParameter(object parameter, out Visibility invisible, out bool isRevert)
  115. {
  116. isRevert = false;
  117. invisible = Visibility.Collapsed;
  118. if (parameter is BoolToVisibilityEnums)
  119. {
  120. BoolToVisibilityEnums options = (BoolToVisibilityEnums)parameter;
  121. if ((options & BoolToVisibilityEnums.Revert) != 0)
  122. {
  123. isRevert = true;
  124. }
  125. if ((options & BoolToVisibilityEnums.InvisibleHidden) != 0)
  126. {
  127. invisible = Visibility.Hidden;
  128. }
  129. }
  130. }
  131. #endregion Methods 
  132. }
  133. }