Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve the usage experience with CompareConverter in XAML #1841

Merged
merged 6 commits into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using CommunityToolkit.Maui.Converters;

namespace CommunityToolkit.Maui.Sample.Converters;

/// <summary>
/// Compares a double value against the ComparingValue property
/// and returns a <see cref="Color"/> based on the comparison.
/// </summary>
public sealed class CompareDoubleToColorConverter : CompareConverter<double, Color>
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="CommunityToolkit.Maui.Sample.Pages.Converters.CompareConverterPage"
xmlns:vm="clr-namespace:CommunityToolkit.Maui.Sample.ViewModels.Converters"
xmlns:converters="clr-namespace:CommunityToolkit.Maui.Sample.Converters"
xmlns:mct="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
xmlns:pages="clr-namespace:CommunityToolkit.Maui.Sample.Pages"
x:TypeArguments="vm:CompareConverterViewModel"
Expand All @@ -12,6 +13,13 @@
<x:Double x:Key="ComparingValue">0.5</x:Double>
<Color x:Key="LightGreen">LightGreen</Color>
<Color x:Key="PaleVioletRed">PaleVioletRed</Color>

<converters:CompareDoubleToColorConverter
x:Key="isLargerThanOneConverter"
ComparingValue="1"
ComparisonOperator="Greater"
TrueObject="LightGreen"
FalseObject="PaleVioletRed" />
</ResourceDictionary>
</pages:BasePage.Resources>

Expand All @@ -22,14 +30,14 @@
TextColor="{StaticResource NormalLabelTextColor}"/>

<Slider
Maximum="1"
Maximum="10"
Minimum="0"
Value="{Binding SliderValue, Mode=TwoWay}"
HorizontalOptions="FillAndExpand"/>

<Label
Text="The background of this label will be green if the value of the slider is greater than or equal to 50%, and red otherwise."
BackgroundColor="{Binding SliderValue, Mode=OneWay, Converter={mct:CompareConverter ComparingValue={StaticResource ComparingValue}, ComparisonOperator=GreaterOrEqual, TrueObject={StaticResource LightGreen}, FalseObject={StaticResource PaleVioletRed}}}"
Text="The background of this label will be green if the value of the slider is greater than 1, and red otherwise."
BackgroundColor="{Binding SliderValue, Mode=OneWay, Converter={StaticResource isLargerThanOneConverter}}"
TextColor="Black"
Padding="4, 0"/>

Expand Down
12 changes: 6 additions & 6 deletions src/CommunityToolkit.Maui/Converters/CompareConverter.shared.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ namespace CommunityToolkit.Maui.Converters;
/// <summary>
/// Converts an object that implements IComparable to an object or a boolean based on a comparison.
/// </summary>
public sealed class CompareConverter : CompareConverter<object>
public sealed class CompareConverter : CompareConverter<IComparable, object>
{
}

/// <summary>
/// Converts an object that implements IComparable to an object or a boolean based on a comparison.
/// </summary>
public abstract class CompareConverter<TObject> : BaseConverterOneWay<IComparable, object>
public abstract class CompareConverter<TValue, TReturnObject> : BaseConverterOneWay<TValue, object> where TValue : IComparable
{
/// <inheritdoc/>
public override object DefaultConvertReturnValue { get; set; } = new();
Expand Down Expand Up @@ -59,7 +59,7 @@ public enum OperatorType
/// <summary>
/// The comparing value.
/// </summary>
public IComparable? ComparingValue { get; set; }
public TValue? ComparingValue { get; set; }

/// <summary>
/// The comparison operator.
Expand All @@ -69,12 +69,12 @@ public enum OperatorType
/// <summary>
/// The object that corresponds to True value.
/// </summary>
public TObject? TrueObject { get; set; }
public TReturnObject? TrueObject { get; set; }

/// <summary>
/// The object that corresponds to False value.
/// </summary>
public TObject? FalseObject { get; set; }
public TReturnObject? FalseObject { get; set; }

/// <summary>
/// Converts an object that implements IComparable to a specified object or a boolean based on a comparison result.
Expand All @@ -83,7 +83,7 @@ public enum OperatorType
/// <param name="culture">The culture to use in the converter. This is not implemented.</param>
/// <returns>The object assigned to <see cref="TrueObject"/> if (value <see cref="ComparisonOperator"/> <see cref="ComparingValue"/>) equals True and <see cref="TrueObject"/> is not null, if <see cref="TrueObject"/> is null it returns true, otherwise the value assigned to <see cref="FalseObject"/>, if no value is assigned then it returns false.</returns>
[MemberNotNull(nameof(ComparingValue))]
public override object ConvertFrom(IComparable value, CultureInfo? culture = null)
public override object ConvertFrom(TValue value, CultureInfo? culture = null)
{
ArgumentNullException.ThrowIfNull(value);
ArgumentNullException.ThrowIfNull(ComparingValue);
Expand Down