|
| 1 | +//---------------------------------------------------------------------------------------------- |
| 2 | +// <copyright file="BooleanConverter.cs" company="Microsoft Corporation"> |
| 3 | +// Licensed under the MIT License. See LICENSE.TXT in the project root license information. |
| 4 | +// </copyright> |
| 5 | +//---------------------------------------------------------------------------------------------- |
| 6 | +using System; |
| 7 | +using System.Collections.Generic; |
| 8 | +using System.Globalization; |
| 9 | +using System.Windows; |
| 10 | +using System.Windows.Data; |
| 11 | + |
| 12 | +namespace SampleDeviceCollection |
| 13 | +{ |
| 14 | + //------------------------------------------------------------------- |
| 15 | + // Boolean Converter |
| 16 | + //------------------------------------------------------------------- |
| 17 | + #region Boolean Converter |
| 18 | + /// <summary> |
| 19 | + /// Template allows for easy creation of a value converter for bools |
| 20 | + /// </summary> |
| 21 | + /// <typeparam name="T">Type to convert back and forth to boolean</typeparam> |
| 22 | + /// <remarks> |
| 23 | + /// See BooleanToVisibilityConverter and BooleanToBrushConverter (below) and usage in Generic.xaml |
| 24 | + /// </remarks> |
| 25 | + public class BooleanConverter<T> : IValueConverter |
| 26 | + { |
| 27 | + /// <summary> |
| 28 | + /// Initializes a new instance of the <see cref="BooleanConverter{T}" /> class. |
| 29 | + /// </summary> |
| 30 | + /// <param name="trueValue">The value of type T that represents true</param> |
| 31 | + /// <param name="falseValue">The value of type T that represents false</param> |
| 32 | + public BooleanConverter(T trueValue, T falseValue) |
| 33 | + { |
| 34 | + this.True = trueValue; |
| 35 | + this.False = falseValue; |
| 36 | + } |
| 37 | + |
| 38 | + /// <summary> |
| 39 | + /// Gets or sets the value that represents true |
| 40 | + /// </summary> |
| 41 | + public T True { get; set; } |
| 42 | + |
| 43 | + /// <summary> |
| 44 | + /// Gets or sets the value that represetns false |
| 45 | + /// </summary> |
| 46 | + public T False { get; set; } |
| 47 | + |
| 48 | + /// <summary> |
| 49 | + /// Convert an object of type T to boolean |
| 50 | + /// </summary> |
| 51 | + /// <param name="value">Object of type T to convert</param> |
| 52 | + /// <param name="targetType">The parameter is not used.</param> |
| 53 | + /// <param name="parameter">The parameter is not used.</param> |
| 54 | + /// <param name="culture">The parameter is not used.</param> |
| 55 | + /// <returns>Object of boolean value</returns> |
| 56 | + public virtual object Convert(object value, Type targetType, object parameter, CultureInfo culture) |
| 57 | + { |
| 58 | + return value is bool && ((bool)value) ? this.True : this.False; |
| 59 | + } |
| 60 | + |
| 61 | + /// <summary> |
| 62 | + /// Convert a boolean value to an object of type T |
| 63 | + /// </summary> |
| 64 | + /// <param name="value">The boolean value to convert</param> |
| 65 | + /// <param name="targetType">The parameter is not used.</param> |
| 66 | + /// <param name="parameter">The parameter is not used.</param> |
| 67 | + /// <param name="culture">The parameter is not used.</param> |
| 68 | + /// <returns>Object of type T</returns> |
| 69 | + public virtual object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) |
| 70 | + { |
| 71 | + return value is T && EqualityComparer<T>.Default.Equals((T)value, this.True); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + /// <summary> |
| 76 | + /// Converter between a boolean and visibility value |
| 77 | + /// </summary> |
| 78 | + [type: System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.MaintainabilityRules", "SA1402:FileMayOnlyContainASingleClass", Justification = "Small classes are all instances of the same generic and are better organized in a single file.")] |
| 79 | + public sealed class BooleanToVisibilityConverter : BooleanConverter<Visibility> |
| 80 | + { |
| 81 | + /// <summary> |
| 82 | + /// Initializes a new instance of the <see cref="BooleanToVisibilityConverter" /> class. |
| 83 | + /// </summary> |
| 84 | + public BooleanToVisibilityConverter() : |
| 85 | + base(Visibility.Visible, Visibility.Hidden) |
| 86 | + { |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + /// <summary> |
| 91 | + /// Converter between a boolean and either "http" or "https" |
| 92 | + /// </summary> |
| 93 | + [type: System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.MaintainabilityRules", "SA1402:FileMayOnlyContainASingleClass", Justification = "Small classes are all instances of the same generic and are better organized in a single file.")] |
| 94 | + public sealed class BooleanToHttpsConverter : BooleanConverter<string> |
| 95 | + { |
| 96 | + /// <summary> |
| 97 | + /// Initializes a new instance of the <see cref="BooleanToHttpsConverter" /> class. |
| 98 | + /// </summary> |
| 99 | + public BooleanToHttpsConverter() : |
| 100 | + base("https", "http") |
| 101 | + { |
| 102 | + } |
| 103 | + } |
| 104 | + #endregion // Boolean Converter |
| 105 | +} |
0 commit comments