InverseBoolToVisibilityConverter.cs 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 InverseBoolToVisibilityConverter : 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. throw new ArgumentException("Invalid argument/return type. Expected argument: bool and return type: Visibility");
  46. }
  47. /// <summary>
  48. /// Converts a value.
  49. /// </summary>
  50. /// <param name="value">The value that is produced by the binding target.</param>
  51. /// <param name="targetType">The type to convert to.</param>
  52. /// <param name="parameter">The converter parameter to use.</param>
  53. /// <param name="culture">The culture to use in the converter.</param>
  54. /// <returns>
  55. /// A converted value. If the method returns null, the valid null value is used.
  56. /// </returns>
  57. public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  58. {
  59. if (value is Visibility && targetType == typeof(bool))
  60. {
  61. Visibility val = (Visibility)value;
  62. if (val == Visibility.Visible)
  63. return false;
  64. else
  65. return true;
  66. }
  67. throw new ArgumentException("Invalid argument/return type. Expected argument: Visibility and return type: bool");
  68. }
  69. #endregion
  70. }
  71. }