BoolToVisibilityConverter.cs 3.9 KB

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