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] `bool`-to-`object` Converter

See original GitHub issue

BoolToObjectConverter

  • Proposed
  • Prototype
  • Implementation
    • iOS Support
    • Android Support
    • macOS Support
    • Windows Support
  • Unit Tests
  • Documentation
  • Sample

Summary

The BoolToObjectConverter is a converter that allows users to convert a bool value binding to a specific object. By providing both a TrueObject and a FalseObject in the converter the appropriate object will be used depending on the value of the binding

Detailed Design

public class BoolToObjectConverter : BoolToObjectConverter<object>
{
}

public class BoolToObjectConverter<TObject> : ValueConverterExtension, IValueConverter
{
  public TObject? TrueObject { get; set; }
  public TObject? FalseObject { get; set; }

  public object? Convert(object? value, Type? targetType, object? parameter, CultureInfo? culture)
  {
	  if (value is bool result)
		  return result ? TrueObject : FalseObject;
  
	  throw new ArgumentException("Value is not a valid boolean", nameof(value));
  }

  public object ConvertBack(object? value, Type? targetType, object? parameter, CultureInfo? culture)
  {
	  if (value is TObject result)
		  return result.Equals(TrueObject);
  
	  if (default(TObject) == null && value == null && TrueObject == null)
		  return true;
  
	  throw new ArgumentException($"Value is not a valid {typeof(TObject).Name}", nameof(value));
  }
}

Usage Syntax

XAML Usage

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:xct="http://xamarin.com/schemas/2020/toolkit"
             x:Class="MyLittleApp.MainPage">

    <ContentPage.Resources>
         <ResourceDictionary>
             <xct:BoolToObjectConverter x:Key="BoolToObjectConverter" TrueObject="16" FalseObject="10" />
         </ResourceDictionary>
    </ContentPage.Resources>

    <StackLayout>

        <Label Text="Hi there from the Docs!" FontSize="{Binding MyBoolean, Converter={StaticResource BoolToObjectConverter}}" />

    </StackLayout>
</ContentPage>

C# Usage

class MyPage : ContentPage
{
    public MyPage()
    {
        Content = new Label { Text = "Hi there from the Docs!"}
                          .Bind(Label.FontSizeProperty, nameof(ViewModel.MyBoolean), converter: new BoolToObjectConverter<double>{ FalseObject = 10, TrueObject = 16 })
    }
}

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
brminnickcommented, Apr 15, 2022

follow-the-rules

1reaction
jfversluiscommented, Apr 15, 2022

Happy now?!

Read more comments on GitHub >

github_iconTop Results From Across the Web

bool-to-object-converter.md
The BoolToObjectConverter is a converter that allows users to convert a bool value binding to a specific object.
Read more >
BoolToObjectConverter - .NET MAUI Community Toolkit
The BoolToObjectConverter is a converter that allows users to convert a bool value binding to a specific object.
Read more >
BoolToObjectConverterPage.xaml
The .NET MAUI Community Toolkit is a community-created library that contains .NET MAUI Extensions, Advanced UI/UX Controls, and Behaviors to help make your ......
Read more >
Syncfusion .NET MAUI Value Converters
The BoolToObjectConverter can convert a Boolean value to an object. By including both a TrueObject and a FalseObject in the converter, the appropriate...
Read more >
Xamarin Community Toolkit BoolToObjectConverter
The BoolToObjectConverter is a converter that allows users to convert a bool value binding to a specific object.
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