|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +using System; |
| 6 | +using Windows.UI.Xaml; |
| 7 | +using Windows.UI.Xaml.Data; |
| 8 | + |
| 9 | +namespace Microsoft.Toolkit.Uwp.UI.Converters |
| 10 | +{ |
| 11 | + /// <summary> |
| 12 | + /// This class returns an object or another, depending on whether the type of the provided value matches another provided Type. |
| 13 | + /// </summary> |
| 14 | + public class TypeToObjectConverter : DependencyObject, IValueConverter |
| 15 | + { |
| 16 | + /// <summary> |
| 17 | + /// Identifies the <see cref="TrueValue"/> property. |
| 18 | + /// </summary> |
| 19 | + public static readonly DependencyProperty TrueValueProperty = |
| 20 | + DependencyProperty.Register(nameof(TrueValue), typeof(object), typeof(TypeToObjectConverter), new PropertyMetadata(null)); |
| 21 | + |
| 22 | + /// <summary> |
| 23 | + /// Identifies the <see cref="FalseValue"/> property. |
| 24 | + /// </summary> |
| 25 | + public static readonly DependencyProperty FalseValueProperty = |
| 26 | + DependencyProperty.Register(nameof(FalseValue), typeof(object), typeof(TypeToObjectConverter), new PropertyMetadata(null)); |
| 27 | + |
| 28 | + /// <summary> |
| 29 | + /// Identifies the <see cref="Type"/> property. |
| 30 | + /// </summary> |
| 31 | + public static readonly DependencyProperty TypeProperty = |
| 32 | + DependencyProperty.Register(nameof(Type), typeof(Type), typeof(TypeToObjectConverter), new PropertyMetadata(typeof(object))); |
| 33 | + |
| 34 | + /// <summary> |
| 35 | + /// Gets or sets the value to be returned when the type of the provided value matches <see cref="Type"/>. |
| 36 | + /// </summary> |
| 37 | + public object TrueValue |
| 38 | + { |
| 39 | + get { return GetValue(TrueValueProperty); } |
| 40 | + set { SetValue(TrueValueProperty, value); } |
| 41 | + } |
| 42 | + |
| 43 | + /// <summary> |
| 44 | + /// Gets or sets the value to be returned when the type of the provided value does not match <see cref="Type"/>. |
| 45 | + /// </summary> |
| 46 | + public object FalseValue |
| 47 | + { |
| 48 | + get { return GetValue(FalseValueProperty); } |
| 49 | + set { SetValue(FalseValueProperty, value); } |
| 50 | + } |
| 51 | + |
| 52 | + /// <summary> |
| 53 | + /// Gets or sets the Type used to compare the type of the provided value. |
| 54 | + /// </summary> |
| 55 | + public Type Type |
| 56 | + { |
| 57 | + get { return (Type)GetValue(TypeProperty); } |
| 58 | + set { SetValue(TypeProperty, value); } |
| 59 | + } |
| 60 | + |
| 61 | + /// <summary> |
| 62 | + /// Convert the <paramref name="value"/>'s Type to an other object. |
| 63 | + /// </summary> |
| 64 | + /// <param name="value">The source data being passed to the target.</param> |
| 65 | + /// <param name="targetType">The type of the target property, as a type reference.</param> |
| 66 | + /// <param name="parameter">An optional parameter to be used to invert the converter logic.</param> |
| 67 | + /// <param name="language">The language of the conversion.</param> |
| 68 | + /// <returns>The value to be passed to the target dependency property.</returns> |
| 69 | + public object Convert(object value, Type targetType, object parameter, string language) |
| 70 | + { |
| 71 | + var typeMatches = value != null && Type.Equals(value.GetType()); |
| 72 | + |
| 73 | + // Negate if needed |
| 74 | + if (ConverterTools.TryParseBool(parameter)) |
| 75 | + { |
| 76 | + typeMatches = !typeMatches; |
| 77 | + } |
| 78 | + |
| 79 | + return ConverterTools.Convert(typeMatches ? TrueValue : FalseValue, targetType); |
| 80 | + } |
| 81 | + |
| 82 | + /// <summary> |
| 83 | + /// Not implemented. |
| 84 | + /// </summary> |
| 85 | + /// <param name="value">The source data being passed to the target.</param> |
| 86 | + /// <param name="targetType">The type of the target property, as a type reference.</param> |
| 87 | + /// <param name="parameter">Optional parameter. Not used.</param> |
| 88 | + /// <param name="language">The language of the conversion. Not used.</param> |
| 89 | + /// <returns>The value to be passed to the target dependency property.</returns> |
| 90 | + public object ConvertBack(object value, Type targetType, object parameter, string language) |
| 91 | + { |
| 92 | + throw new NotImplementedException(); |
| 93 | + } |
| 94 | + } |
| 95 | +} |
0 commit comments