question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Proposal] Variable Multi-Value Converter

See original GitHub issue

VariableMultiValueConverter

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:closed
  • Created 2 years ago
  • Comments:12 (11 by maintainers)

github_iconTop GitHub Comments

1reaction
bijingtoncommented, May 3, 2022

Done thanks @CliffAgius

1reaction
brminnickcommented, Feb 23, 2022

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 🤷‍♂️

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found