Skip to content

Compare Markup Extension

Overview

The Compare markup extension is a XAML-friendly wrapper around the CommunityToolkit.Maui CompareConverter. It simplifies the comparison of values and returns one of two objects, depending on whether the value of the comparison.

Unlike the underlying converter, all properties exposed by the markup extension are bindable. This allows values to be supplied through data bindings and ensures the conversion result automatically updates whenever the source value, ComparingValue, ComparisonOperator, TrueObject, or FalseObject changes.

Experimental Feature

This feature is experimental and has not been fully tested. Its API and behavior may change in future releases, and you may encounter unexpected issues. Use with caution and thoroughly test it in your own application before relying on it in production.

XAML Namespace

xmlns:sqm="clr-namespace:SQuan.Helpers.Maui;assembly=SQuan.Helpers.Maui"

Fields

Name Description
ComparisonOperatorProperty Bindable property for ComparisonOperator
ComparingValueProperty Bindable property for ComparingValue
FalseObjectProperty Bindable property for FalseObject.
TrueObjectProperty Bindable property for TrueObject.
ValueProperty Bindable property for Value.

Properties

Name Description
ComparisonOperator Gets or sets the comparison operator.
ComparingValue Gets or sets the secondary value.
FalseObject Gets or sets the false object.
TrueObject Gets or sets the true object.
Value Gets or sets the primary value.

ComparisonOperator Property

Gets or sets the comparison type. Defaults to OperatorType.Equal.

public CompareConverter.OperatorType ComparisonOperator { get; set; } = CompareConverter.OperatorType.Equal;

ComparingValue Property

Gets or sets a primary value.

public IComparable ComparingValue { get; set; }

FalseObject Property

Gets or sets the value that will be returned if the comparison result is false.

public object? FalseObject { get; set; }

TrueObject Property

Gets or sets the value that will be returned if the comparison result is true.

public object? TrueObject { get; set; }

Value Property

Gets or sets a primary value.

public IComparable Value { get; set; }

Example

<ContentPage
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:sqm="clr-namespace:SQuan.Helpers.Maui;assembly=SQuan.Helpers.Maui">
    <VerticalStackLayout>
        <Slider x:Name="slider" MinimumValue="0" MaximumValue="100" Value="20" />
        <Slider x:Name="slider2" MinimumValue="0" MaximumValue="100" Value="50" />
        <BoxView
            WidthRequest="30"
            HeightRequest="30"
            Color="{sqm:Compare Value={Binding Value,
                                               x:DataType=Slider,
                                               Source={Reference slider}},
                                ComparingValue={Binding Value,
                                                         x:DataType=Slider,
                                                         Source={Reference slider2}},
                                ComparisonOperator=GreaterOrEqual,
                                TrueObject={Binding Source=Green},
                                FalseObject={Binding Source=Red}" />
    </VerticalStackLayout>
</ContentPage>