VariableMultiValueConverter は、ユーザーが bool 値を MultiBinding を介して単一の bool に変換することを可能にするコンバーターです。 これを行うには、ConditionType で指定されているとおりに All、Any、None、または特定の数の値が true かどうかを指定できるようにします。
Convert メソッドは、指定された values を、定義された ConditionType に基づいて全体的な bool の結果に変換して返します。
ConvertBack メソッドは、ConditionType が MultiBindingCondition.All に設定されている場合にのみ結果を返します。
BaseConverter プロパティ
基底クラス public abstract class BaseConverter には、次のプロパティが実装されています。
| プロパティ | 説明 |
|---|---|
DefaultConvertReturnValue |
IValueConverter.Convert(object?, Type, object?, CultureInfo?) が Exception をスローしたときに返される既定値です。 この値は、CommunityToolkit.Maui.Options.ShouldSuppressExceptionsInConverters が true に設定されている場合に使用されます。 |
DefaultConvertBackReturnValue |
IValueConverter.ConvertBack(object?, Type, object?, CultureInfo?) が Exception をスローしたときに返される既定値です。 この値は、CommunityToolkit.Maui.Options.ShouldSuppressExceptionsInConverters が true に設定されている場合に使用されます。 |
ICommunityToolkitValueConverter プロパティ
public interface ICommunityToolkitValueConverter には次のプロパティが実装されています。
| プロパティ | タイプ | 説明 |
|---|---|---|
DefaultConvertReturnValue |
object? |
IValueConverter.Convert(object?, Type, object?, CultureInfo?) が Exception をスローしたときに返される既定値です。 この値は、CommunityToolkit.Maui.Options.ShouldSuppressExceptionsInConverters が true に設定されている場合に使用されます。 |
DefaultConvertBackReturnValue |
object? |
IValueConverter.ConvertBack(object?, Type, object?, CultureInfo?) が Exception をスローしたときに返される既定値です。 この値は、CommunityToolkit.Maui.Options.ShouldSuppressExceptionsInConverters が true に設定されている場合に使用されます。 |
構文
次の例では、MultiBinding の値のうち少なくとも 2 つが true と評価されたときに、Label を非表示にする方法を示します。
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# Markup
この 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
});
}
}
Properties
| プロパティ | タイプ | 説明 |
|---|---|---|
| ConditionType | MultiBindingCondition |
MultiBinding で指定されたブール値のうち、true である必要がある値の数を示します。 |
| カウント | int |
GreaterThan、LessThan、または Exact の ConditionType を使用する場合に true にする必要がある値の数。 |
MultiBindingCondition
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 リポジトリにあります。
.NET MAUI Community Toolkit