[Proposal] IsNull / IsNotNull Converters
See original GitHub issueIsNull IsNotNull Converters
- Proposed
- Prototype
- Implementation
- iOS Support
- Android Support
- macOS Support
- Windows Support
- Unit Tests
- Sample
- Documentation: Done
Link to Discussion
Summary
The IsNullConverter
is a converter that allows users to convert an incoming binding to a bool value. This value represents if the incoming binding value is null.
The IsNotNullConverter
is a converter that allows users to convert an incoming binding to a bool value. This value represents if the incoming binding value is not null.
Motivation
As mentioned in the discussion this was the behaviour of another converter, so these converters would just reimplement the old behaviour. It’s been quite common in the projects I’ve worked on to be able to check any object type if it’s null or not.
Detailed Design
IsNotNullConverter.shared.cs
/// <summary>
/// Converts the incoming value to a <see cref="bool"/> indicating whether or not the value is not null.
/// </summary>
public class IsNotNullConverter : BaseConverter<object?, bool>
{
/// <summary>
/// Converts the incoming object to a <see cref="bool"/> indicating whether or not the value is not null.
/// </summary>
/// <param name="value">The value to convert.</param>
/// <returns>A <see cref="bool"/> indicating if the incoming value is not null</returns>
public bool ConvertFrom(object? value) => value is not null;
}
IsNullConverter.shared.cs
/// <summary>
/// Converts the incoming value to a <see cref="bool"/> indicating whether or not the value is null.
/// </summary>
public class IsNullConverter : BaseConverter<object?, bool>
{
/// <summary>
/// Converts the incoming object to a <see cref="bool"/> indicating whether or not the value is not null.
/// </summary>
/// <param name="value">The value to convert.</param>
/// <returns>A <see cref="bool"/> indicating if the incoming value is not null</returns>
public bool ConvertFrom(object? value) => value is null;
}
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: IsNullConverter x:Key="IsNullConverter" />
</ResourceDictionary>
</ContentPage.Resources>
<StackLayout IsVisible="{Binding MyObject, Converter={StaticResource IsNullConverter}}">
</StackLayout>
</ContentPage>
C# Usage
class MyPage : ContentPage
{
public MyPage()
{
Content = new StackLayout().Bind(StackLayout.IsVisibleProperty, nameof(ViewModel.MyObject), converter: new IsNullConverter())
}
}
Drawbacks
None
Alternatives
None
Issue Analytics
- State:
- Created 2 years ago
- Reactions:2
- Comments:17 (9 by maintainers)
Top Results From Across the Web
CommunityToolkit/docs/maui/converters/is-not-null- ...
The IsNotNullConverter is a converter that allows users to convert an incoming binding to a bool value. This value represents if the incoming ......
Read more >is-string-not-null-or-empty-converter.md
The IsStringNotNullOrEmptyConverter is a one way converter that returns a bool indicating whether the binding value is not null and not an ...
Read more >Deciding between COALESCE and ISNULL in SQL Server
When writing T-SQL, a lot of developers use either COALESCE or ISNULL in order to provide a default value in cases where the...
Read more >DataTrigger where value is NOT null? - wpf
This is a bit of a cheat but I just set a default style and then overrode it using a DataTrigger if the...
Read more >Performance implications of using ISNULL vs IS NULL
This query is looking for any records where the AccountNbr column is empty or NULL. From looking at the execution plan we'll see...
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
Cool no prob, i’ll wait
@brminnick awesome, ye will do