VariableMultiValueConverter

VariableMultiValueConverterは、ユーザーがboolを介してMultiBinding値を 1 つのboolに変換できるコンバーターです。 これを行うには、ConditionType で指定されているとおりに All、Any、None、または特定の数の値が true かどうかを指定できるようにします。

Convert メソッドは、定義されたvaluesに基づいて、指定されたboolを全体的なConditionType結果に変換して返します。

ConvertBack メソッドは、ConditionTypeMultiBindingCondition.All に設定されている場合にのみ結果を返します。

BaseConverter のプロパティ

基底クラスでは、次のプロパティが実装 public abstract class BaseConverter

財産 Description
DefaultConvertReturnValue IValueConverter.Convert(object?, Type, object?, CultureInfo?)Exceptionをスローしたときに返される既定値。 この値は、 CommunityToolkit.Maui.Options.ShouldSuppressExceptionsInConverterstrue に設定されている場合に使用されます。
DefaultConvertBackReturnValue IValueConverter.ConvertBack(object?, Type, object?, CultureInfo?)Exceptionをスローしたときに返される既定値。 この値は、 CommunityToolkit.Maui.Options.ShouldSuppressExceptionsInConverterstrue に設定されている場合に使用されます。

ICommunityToolkitValueConverter プロパティ

public interface ICommunityToolkitValueConverterでは、次のプロパティが実装されています。

財産 タイプ Description
DefaultConvertReturnValue object? IValueConverter.Convert(object?, Type, object?, CultureInfo?)Exceptionをスローしたときに返される既定値。 この値は、 CommunityToolkit.Maui.Options.ShouldSuppressExceptionsInConverterstrue に設定されている場合に使用されます。
DefaultConvertBackReturnValue object? IValueConverter.ConvertBack(object?, Type, object?, CultureInfo?)Exceptionをスローしたときに返される既定値。 この値は、 CommunityToolkit.Maui.Options.ShouldSuppressExceptionsInConverterstrue に設定されている場合に使用されます。

Syntax

次の例では、Labelの値のうち少なくとも 2 つが true と評価されたときに、MultiBindingを非表示にする方法を示します。

XAML

XAML 名前空間を含める

XAML でこのツールキットを使用するには、次の xmlns をページまたはビューに追加する必要があります。

xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"

したがって、以下の通りです

<ContentPage
    x:Class="CommunityToolkit.Maui.Sample.Pages.MyPage"
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml">

</ContentPage>

次のように、xmlns を含むように変更されます。

<ContentPage
    x:Class="CommunityToolkit.Maui.Sample.Pages.MyPage"
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit">

</ContentPage>

VariableMultiValueConverter の使用

VariableMultiValueConverterは、XAML で次のように使用できます。

<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
             x:Class="CommunityToolkit.Maui.Sample.Pages.Converters.VariableMultiValueConverterPage">

    <ContentPage.Resources>
        <ResourceDictionary>
            <toolkit:VariableMultiValueConverter 
                x:Key="VariableMultiValueConverter"
                ConditionType="LessThan"
                Count="2" />
        </ResourceDictionary>
    </ContentPage.Resources>

    <Label Text="At least 2 toppings must be selected.">
        <Label.IsVisible>
            <MultiBinding Converter="{StaticResource VariableMultiValueConverter}">
                <Binding Path="IsCheeseSelected" />
                <Binding Path="IsHamSelected" />
                <Binding Path="IsPineappleSelected" />
            </MultiBinding>
        </Label.IsVisible>
    </Label>

</ContentPage>

C#

VariableMultiValueConverterは、C# で次のように使用できます。


class VariableMultiValueConverterPage : ContentPage
{
    public VariableMultiValueConverterPage()
    {
        var label = new Label
        {
            Text = "At least 2 toppings must be selected."
        };

        label.SetBinding(
            Label.IsVisibleProperty,
            new MultiBinding
            {
                Converter = new VariableMultiValueConverter
                {
                    ConditionType = MultiBindingCondition.LessThan,
                    Count = 2
                },
                Bindings = new List<BindingBase>
                {
                    new Binding(static (ViewModel vm) => vm.IsCheeseSelected),
                    new Binding(static (ViewModel vm) => vmIsHamSelected),
                    new Binding(static (ViewModel vm) => vmIsPineappleSelected)
                }
            });

        Content = label;
    }
}

C# マークアップ

この CommunityToolkit.Maui.Markup パッケージは、C# でこのコンバーターを使用するためのより簡潔な方法を提供します。

using CommunityToolkit.Maui.Markup;

class VariableMultiValueConverterPage : ContentPage
{
    public VariableMultiValueConverterPage()
    {
        Content = new Label()
            .Text("At least 2 toppings must be selected.")
            .Bind(
                Label.IsVisibleProperty,
                new List<BindingBase>
                {
                    new Binding(static (ViewModel vm) => vm.IsCheeseSelected),
                    new Binding(static (ViewModel vm) => vm.IsHamSelected),
                    new Binding(static (ViewModel vm) => vm.IsPineappleSelected)
                },
                converter: new VariableMultiValueConverter
                {
                    ConditionType = MultiBindingCondition.LessThan,
                    Count = 2
                });
    }
}

プロパティ

財産 タイプ Description
ConditionType MultiBindingCondition MultiBinding で指定されたブール値のうち、true である必要がある値の数を示します。
Count int ConditionTypeGreaterThan、またはLessThanExactを使用する場合に true にする必要がある値の数。

複数バインディング条件

MultiBindingCondition 列挙型には、次のメンバーが定義されています。

  • None - どの値も true にすることはできません。
  • All - すべての値が true である必要があります。
  • Any - いずれかの値が true である必要があります。
  • Exact - Count プロパティで構成されている正確な数は true である必要があります。
  • GreaterThan - Count プロパティで構成された数値より大きい場合は true になります。
  • LessThan - Count プロパティで構成されている数値より小さい値は true にする必要があります。

例示

このコンバーターの動作例は、.NET MAUI Community Toolkit サンプル アプリケーションで確認できます。

API

VariableMultiValueConverter のソース コードは、.NET MAUI Community Toolkit GitHub リポジトリで確認できます。