BoolToVisibilityConverter.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows.Data;
  8. using System.Windows;
  9. namespace Compdfkit_Tools.Common
  10. {
  11. /// <summary>
  12. /// Value converter between bool and Visibility
  13. /// </summary>
  14. [ValueConversion(typeof(bool), typeof(Visibility))]
  15. public class BoolToVisibleConverter : IValueConverter
  16. {
  17. #region IValueConverter Members
  18. /// <summary>
  19. /// Converts a value.
  20. /// </summary>
  21. /// <param name="value">The value produced by the binding source.</param>
  22. /// <param name="targetType">The type of the binding target property.</param>
  23. /// <param name="parameter">The converter parameter to use.</param>
  24. /// <param name="culture">The culture to use in the converter.</param>
  25. /// <returns>
  26. /// A converted value. If the method returns null, the valid null value is used.
  27. /// </returns>
  28. public object Convert(
  29. object value,
  30. Type targetType,
  31. object parameter,
  32. CultureInfo culture)
  33. {
  34. if ((bool)value)
  35. {
  36. return Visibility.Visible;
  37. }
  38. else
  39. {
  40. return Visibility.Collapsed;
  41. }
  42. }
  43. /// <summary>
  44. /// Converts a value back.
  45. /// </summary>
  46. /// <param name="value">The value that is produced by the binding target.</param>
  47. /// <param name="targetType">The type to convert to.</param>
  48. /// <param name="parameter">The converter parameter to use.</param>
  49. /// <param name="culture">The culture to use in the converter.</param>
  50. /// <returns>
  51. /// A converted value. If the method returns null, the valid null value is used.
  52. /// </returns>
  53. public object ConvertBack(
  54. object value,
  55. Type targetType,
  56. object parameter,
  57. CultureInfo culture)
  58. {
  59. return (Visibility)value == Visibility.Visible;
  60. }
  61. #endregion IValueConverter Members
  62. }
  63. /// <summary>
  64. /// Value converter between bool and Visibility
  65. /// </summary>
  66. [ValueConversion(typeof(bool), typeof(Visibility))]
  67. public class BoolToCollapsedConverter : IValueConverter
  68. {
  69. #region IValueConverter Members
  70. /// <summary>
  71. /// Converts a value.
  72. /// </summary>
  73. /// <param name="value">The value produced by the binding source.</param>
  74. /// <param name="targetType">The type of the binding target property.</param>
  75. /// <param name="parameter">The converter parameter to use.</param>
  76. /// <param name="culture">The culture to use in the converter.</param>
  77. /// <returns>
  78. /// A converted value. If the method returns null, the valid null value is used.
  79. /// </returns>
  80. public object Convert(
  81. object value,
  82. Type targetType,
  83. object parameter,
  84. CultureInfo culture)
  85. {
  86. if ((bool)value)
  87. {
  88. return Visibility.Collapsed;
  89. }
  90. else
  91. {
  92. return Visibility.Visible;
  93. }
  94. }
  95. /// <summary>
  96. /// Converts a value back.
  97. /// </summary>
  98. /// <param name="value">The value that is produced by the binding target.</param>
  99. /// <param name="targetType">The type to convert to.</param>
  100. /// <param name="parameter">The converter parameter to use.</param>
  101. /// <param name="culture">The culture to use in the converter.</param>
  102. /// <returns>
  103. /// A converted value. If the method returns null, the valid null value is used.
  104. /// </returns>
  105. public object ConvertBack(
  106. object value,
  107. Type targetType,
  108. object parameter,
  109. CultureInfo culture)
  110. {
  111. return null;
  112. }
  113. #endregion IValueConverter Members
  114. }
  115. }