[Proposal] Variable Multi-Value Converter
See original GitHub issueVariableMultiValueConverter
- Proposed
- Prototype
- Implementation
- iOS Support
- Android Support
- macOS Support
- Windows Support
- Unit Tests
- Sample
- Documentation:https://github.com/MicrosoftDocs/CommunityToolkit/pull/108
Summary
The VariableMultiValueConverter
is a converter that allows users to convert multiple bool
value bindings to a single bool
. It does this by enabling them to specify whether All, Any, None or a specific number of values are true as specified in ConditionType
. This is useful when combined with MultiBinding
Detailed Design
VariableMultiValueConverter.shared.cs
public class VariableMultiValueConverter : MultiValueConverterExtension, IMultiValueConverter
{
public MultiBindingCondition ConditionType { get; set; }
public int Count { get; set; }
public object Convert(object[]? values, Type? targetType, object? parameter, CultureInfo? culture)
{
if (values == null || values.Length == 0)
return false;
var boolValues = values.OfType<bool>().ToArray();
if (boolValues.Length != values.Length)
return false;
var count = boolValues.Count(v => v);
return ConditionType switch
{
MultiBindingCondition.Any => count >= 1,
MultiBindingCondition.None => count == 0,
MultiBindingCondition.Exact => count == Count,
MultiBindingCondition.GreaterThan => count > Count,
MultiBindingCondition.LessThan => count < Count,
_ => count == boolValues.Count(),
};
}
public object[]? ConvertBack(object? value, Type[]? targetTypes, object? parameter, CultureInfo? culture)
{
if (value is not bool boolValue || targetTypes?.Any(t => !t.IsAssignableFrom(typeof(bool))) is true)
return null;
return boolValue ? targetTypes?.Select(t => ConditionType == MultiBindingCondition.All).OfType<object>().ToArray() : null;
}
}
Usage Syntax
Issue Analytics
- State:
- Created 2 years ago
- Comments:12 (11 by maintainers)
Top Results From Across the Web
CommunityToolkit/docs/maui/converters/variable-multi- ...
The VariableMultiValueConverter is a converter that allows users to convert bool values via a MultiBinding to a single bool . It does this...
Read more >VariableMultiValueConverter - .NET MAUI Community Toolkit
The VariableMultiValueConverter is a converter that allows users to convert bool values via a MultiBinding to a single bool .
Read more >How to pass multiple value to a IValueConverter
Hi, I'm using a IValueConverter to assign a color to a ProgressBarEdit. To set the good color to the progress bar I need...
Read more >Multiple bindings as parameter for converter WPF MVVM
Assuming that the color of the ellipse changes from multiple conditions you can use the IMultiValueConverter interface. For example you have ...
Read more >Introduction to Multi Binding and Multi value converter using ...
In the following code, AddConverter implements the IMultiValueConverter interface. AddConverter takes the values from the individual bindings ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Done thanks @CliffAgius
Thanks! Yea, GitHub has a weird quirk that we can’t assign an Issue to anyone who isn’t a maintainer or who hasn’t commented directly on the Issue 🤷♂️