BoolToVisibilityConverter.cs 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*************************************************************************************
  2. Extended WPF Toolkit
  3. Copyright (C) 2007-2013 Xceed Software Inc.
  4. This program is provided to you under the terms of the Microsoft Public
  5. License (Ms-PL) as published at http://wpftoolkit.codeplex.com/license
  6. For more features, controls, and fast professional support,
  7. pick up the Plus Edition at http://xceed.com/wpf_toolkit
  8. Stay informed: follow @datagrid on Twitter or Like http://facebook.com/datagrids
  9. ***********************************************************************************/
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Linq;
  13. using System.Text;
  14. using System.Windows.Data;
  15. using System.Windows;
  16. namespace Xceed.Wpf.AvalonDock.Converters
  17. {
  18. [ValueConversion(typeof(bool), typeof(Visibility))]
  19. public class BoolToVisibilityConverter : IValueConverter
  20. {
  21. #region IValueConverter Members
  22. /// <summary>
  23. /// Converts a value.
  24. /// </summary>
  25. /// <param name="value">The value produced by the binding source.</param>
  26. /// <param name="targetType">The type of the binding target property.</param>
  27. /// <param name="parameter">The converter parameter to use.</param>
  28. /// <param name="culture">The culture to use in the converter.</param>
  29. /// <returns>
  30. /// A converted value. If the method returns null, the valid null value is used.
  31. /// </returns>
  32. public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  33. {
  34. if (value is bool && targetType == typeof(Visibility))
  35. {
  36. bool val = (bool)value;
  37. if (val)
  38. return Visibility.Visible;
  39. else
  40. if (parameter != null && parameter is Visibility)
  41. return parameter;
  42. else
  43. return Visibility.Collapsed;
  44. }
  45. if (value == null)
  46. {
  47. if (parameter != null && parameter is Visibility)
  48. return parameter;
  49. else
  50. return Visibility.Collapsed;
  51. }
  52. return Visibility.Visible;
  53. ///throw new ArgumentException("Invalid argument/return type. Expected argument: bool and return type: Visibility");
  54. }
  55. /// <summary>
  56. /// Converts a value.
  57. /// </summary>
  58. /// <param name="value">The value that is produced by the binding target.</param>
  59. /// <param name="targetType">The type to convert to.</param>
  60. /// <param name="parameter">The converter parameter to use.</param>
  61. /// <param name="culture">The culture to use in the converter.</param>
  62. /// <returns>
  63. /// A converted value. If the method returns null, the valid null value is used.
  64. /// </returns>
  65. public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  66. {
  67. if (value is Visibility && targetType == typeof(bool))
  68. {
  69. Visibility val = (Visibility)value;
  70. if (val == Visibility.Visible)
  71. return true;
  72. else
  73. return false;
  74. }
  75. throw new ArgumentException("Invalid argument/return type. Expected argument: Visibility and return type: bool");
  76. }
  77. #endregion
  78. }
  79. }