|
| 1 | +/* |
| 2 | + * COPYRIGHT: See COPYING in the top level directory |
| 3 | + * PROJECT: CommonControls.Converter |
| 4 | + * FILE: BooleanToVisibilityConverter.cs |
| 5 | + * PURPOSE: Boolean to Visibility converter. |
| 6 | + * PROGRAMMER: Peter Geinitz (Wayfarer) |
| 7 | + */ |
| 8 | + |
| 9 | +using System; |
| 10 | +using System.Globalization; |
| 11 | +using System.Windows; |
| 12 | +using System.Windows.Data; |
| 13 | + |
| 14 | +namespace CommonControls.Converter |
| 15 | +{ |
| 16 | + /// <summary> |
| 17 | + /// Boolean to Visibility converter. |
| 18 | + /// </summary> |
| 19 | + /// <seealso cref="System.Windows.Data.IValueConverter" /> |
| 20 | + public class BooleanToVisibilityConverter : IValueConverter |
| 21 | + { |
| 22 | + /// <summary> |
| 23 | + /// Gets or sets a value indicating whether this <see cref="BooleanToVisibilityConverter"/> is collapse. |
| 24 | + /// </summary> |
| 25 | + /// <value> |
| 26 | + /// <c>true</c> if collapse; otherwise, <c>false</c>. |
| 27 | + /// </value> |
| 28 | + public bool Collapse { get; set; } = false; // true → Collapsed, false → Hidden |
| 29 | + |
| 30 | + /// <summary> |
| 31 | + /// Converts a value. |
| 32 | + /// </summary> |
| 33 | + /// <param name="value">The value produced by the binding source.</param> |
| 34 | + /// <param name="targetType">The type of the binding target property.</param> |
| 35 | + /// <param name="parameter">The converter parameter to use.</param> |
| 36 | + /// <param name="culture">The culture to use in the converter.</param> |
| 37 | + /// <returns> |
| 38 | + /// A converted value. If the method returns <see langword="null" />, the valid null value is used. |
| 39 | + /// </returns> |
| 40 | + public object Convert(object value, Type targetType, object parameter, CultureInfo culture) |
| 41 | + { |
| 42 | + if (value is bool b) |
| 43 | + return b ? Visibility.Visible : (Collapse ? Visibility.Collapsed : Visibility.Hidden); |
| 44 | + return Visibility.Hidden; |
| 45 | + } |
| 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 <see langword="null" />, the valid null value is used. |
| 56 | + /// </returns> |
| 57 | + public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) |
| 58 | + => value is Visibility v && v == Visibility.Visible; |
| 59 | + } |
| 60 | +} |
0 commit comments