Skip to content

Commit c3279e1

Browse files
Implement [BindableProperty] for Converters (CommunityToolkit#3018)
Implement `[BindableProperty]`
1 parent 50fa459 commit c3279e1

File tree

2 files changed

+25
-43
lines changed

2 files changed

+25
-43
lines changed

src/CommunityToolkit.Maui.UnitTests/Converters/IsInRangeConverterTests.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Globalization;
22
using CommunityToolkit.Maui.Converters;
3+
using CommunityToolkit.Maui.Core;
34
using Xunit;
45

56
namespace CommunityToolkit.Maui.UnitTests.Converters;
@@ -552,4 +553,17 @@ public void ReturnObjectsNullExpectBoolReturn(IComparable value, IComparable com
552553
object convertFromResult = isInRangeConverter.ConvertFrom(value, CultureInfo.CurrentCulture);
553554
Assert.Equal(expectedResult, convertFromResult);
554555
}
556+
557+
[Fact]
558+
public void VerifyDefaultValues()
559+
{
560+
// Arrange
561+
var converter = new IsInRangeConverter();
562+
563+
// Act Assert
564+
Assert.Null(converter.FalseObject);
565+
Assert.Null(converter.TrueObject);
566+
Assert.Null(converter.MinValue);
567+
Assert.Null(converter.MaxValue);
568+
}
555569
}

src/CommunityToolkit.Maui/Converters/IsInRangeConverter.shared.cs

Lines changed: 11 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -3,63 +3,31 @@
33

44
namespace CommunityToolkit.Maui.Converters;
55

6-
/// <summary>Converts the incoming value to a <see cref="bool"/> indicating whether or not the value is within a range.</summary>
6+
/// <summary>Converts the incoming value to a <see cref="bool"/> indicating whether the value is within a range.</summary>
77
[AcceptEmptyServiceProvider]
88
public sealed partial class IsInRangeConverter : IsInRangeConverter<IComparable, object>;
99

10-
/// <summary>Converts the incoming value to a <see cref="bool"/> indicating whether or not the value is within a range.</summary>
11-
public abstract class IsInRangeConverter<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] TValue, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] TReturnObject> : BaseConverterOneWay<TValue, object> where TValue : IComparable
10+
/// <summary>Converts the incoming value to a <see cref="bool"/> indicating whether the value is within a range.</summary>
11+
public abstract partial class IsInRangeConverter<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] TValue, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] TReturnObject> : BaseConverterOneWay<TValue, object> where TValue : IComparable
1212
{
13-
/// <summary>
14-
/// Bindable property for <see cref="FalseObject"/>
15-
/// </summary>
16-
public static readonly BindableProperty FalseObjectProperty = BindableProperty.Create(nameof(FalseObject), typeof(TReturnObject?), typeof(IsInRangeConverter<TValue, TReturnObject>));
17-
18-
/// <summary>
19-
/// Bindable property for <see cref="MaxValue"/>
20-
/// </summary>
21-
public static readonly BindableProperty MaxValueProperty = BindableProperty.Create(nameof(MaxValue), typeof(TValue), typeof(IsInRangeConverter<TValue, TReturnObject>));
22-
23-
/// <summary>
24-
/// Bindable property for <see cref="MinValue"/>
25-
/// </summary>
26-
public static readonly BindableProperty MinValueProperty = BindableProperty.Create(nameof(MinValue), typeof(TValue), typeof(IsInRangeConverter<TValue, TReturnObject>));
27-
28-
/// <summary>
29-
/// Bindable property for <see cref="TrueObject"/>
30-
/// </summary>
31-
public static readonly BindableProperty TrueObjectProperty = BindableProperty.Create(nameof(TrueObject), typeof(TReturnObject?), typeof(IsInRangeConverter<TValue, TReturnObject>));
32-
3313
/// <inheritdoc/>
3414
public override object DefaultConvertReturnValue { get; set; } = new();
3515

3616
/// <summary>If supplied this value will be returned when the converter receives an input value that is <b>outside</b> the <see cref="MinValue" /> and <see cref="MaxValue" />s.</summary>
37-
public TReturnObject? FalseObject
38-
{
39-
get => (TReturnObject?)GetValue(FalseObjectProperty);
40-
set => SetValue(FalseObjectProperty, value);
41-
}
17+
[BindableProperty]
18+
public partial TReturnObject? FalseObject { get; set; }
4219

4320
/// <summary>The upper bounds of the range to compare against when determining whether the input value to the convert is within range.</summary>
44-
public TValue? MaxValue
45-
{
46-
get => (TValue?)GetValue(MaxValueProperty);
47-
set => SetValue(MaxValueProperty, value);
48-
}
21+
[BindableProperty]
22+
public partial TValue? MaxValue { get; set; }
4923

5024
/// <summary>The lower bounds of the range to compare against when determining whether the input value to the convert is within range.</summary>
51-
public TValue? MinValue
52-
{
53-
get => (TValue?)GetValue(MinValueProperty);
54-
set => SetValue(MinValueProperty, value);
55-
}
25+
[BindableProperty]
26+
public partial TValue? MinValue { get; set; }
5627

5728
/// <summary>If supplied this value will be returned when the converter receives an input value that is <b>inside</b> (inclusive) of the <see cref="MinValue" /> and <see cref="MaxValue" />s.</summary>
58-
public TReturnObject? TrueObject
59-
{
60-
get => (TReturnObject?)GetValue(TrueObjectProperty);
61-
set => SetValue(TrueObjectProperty, value);
62-
}
29+
[BindableProperty]
30+
public partial TReturnObject? TrueObject { get; set; }
6331

6432
/// <summary>Converts an object that implemented IComparable to a <see cref="bool"/> based on the object being within a <see cref="MinValue"/> and <see cref="MaxValue"/> range.</summary>
6533
/// <param name="value">The value to convert.</param>

0 commit comments

Comments
 (0)