[Proposal] `bool`-to-`object` Converter
See original GitHub issueBoolToObjectConverter
- 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:
- Created 2 years ago
- Comments:6 (4 by maintainers)
Top 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 >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
Happy now?!