BooleanToVisibilityConverter.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #if SILVERLIGHT
  2. namespace Caliburn.Micro
  3. {
  4. using System;
  5. using System.Globalization;
  6. using System.Windows;
  7. using System.Windows.Data;
  8. /// <summary>
  9. /// An <see cref="IValueConverter"/> which converts <see cref="bool"/> to <see cref="Visibility"/>.
  10. /// </summary>
  11. public class BooleanToVisibilityConverter : IValueConverter {
  12. /// <summary>
  13. /// Converts a boolean value to a <see cref="Visibility"/> value.
  14. /// </summary>
  15. /// <param name="value">The value produced by the binding source.</param>
  16. /// <param name="targetType">The type of the binding target property.</param>
  17. /// <param name="parameter">The converter parameter to use.</param>
  18. /// <param name="culture">The culture to use in the converter.</param>
  19. /// <returns>
  20. /// A converted value. If the method returns null, the valid null value is used.
  21. /// </returns>
  22. public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
  23. return ((bool)value) ? Visibility.Visible : Visibility.Collapsed;
  24. }
  25. /// <summary>
  26. /// Converts a value <see cref="Visibility"/> value to a boolean value.
  27. /// </summary>
  28. /// <param name="value">The value that is produced by the binding target.</param>
  29. /// <param name="targetType">The type to convert to.</param>
  30. /// <param name="parameter">The converter parameter to use.</param>
  31. /// <param name="culture">The culture to use in the converter.</param>
  32. /// <returns>
  33. /// A converted value. If the method returns null, the valid null value is used.
  34. /// </returns>
  35. public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
  36. return ((Visibility)value) == Visibility.Visible;
  37. }
  38. }
  39. }
  40. #endif
  41. #if WinRT
  42. namespace Caliburn.Micro
  43. {
  44. using System;
  45. using Windows.UI.Xaml;
  46. using Windows.UI.Xaml.Data;
  47. /// <summary>
  48. /// An <see cref="IValueConverter"/> which converts <see cref="bool"/> to <see cref="Visibility"/>.
  49. /// </summary>
  50. public class BooleanToVisibilityConverter : IValueConverter
  51. {
  52. /// <summary>
  53. /// Converts a boolean value to a <see cref="Visibility"/> value.
  54. /// </summary>
  55. /// <param name="value">The value produced by the binding source.</param>
  56. /// <param name="targetType">The type of the binding target property.</param>
  57. /// <param name="parameter">The converter parameter to use.</param>
  58. /// <param name="language">The language to use in the converter.</param>
  59. /// <returns>
  60. /// A converted value. If the method returns null, the valid null value is used.
  61. /// </returns>
  62. public object Convert(object value, Type targetType, object parameter, string language)
  63. {
  64. return ((bool)value) ? Visibility.Visible : Visibility.Collapsed;
  65. }
  66. /// <summary>
  67. /// Converts a value <see cref="Visibility"/> value to a boolean value.
  68. /// </summary>
  69. /// <param name="value">The value that is produced by the binding target.</param>
  70. /// <param name="targetType">The type to convert to.</param>
  71. /// <param name="parameter">The converter parameter to use.</param>
  72. /// <param name="language">The language 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 ConvertBack(object value, Type targetType, object parameter, string language)
  77. {
  78. return ((Visibility)value) == Visibility.Visible;
  79. }
  80. }
  81. }
  82. #endif